Python list operation

 



What is list ?

A list is a data structure in python that is mutable or you can say changeable. List can store any value of multiple data types. List are defined using square braces []. All the elements inside a list is called items.

How many list operations are there in python ?

There are total eight operations present in Python.


Suppose your created list is defined as 'l' , then:-
l=[ 'a', 'b', 'c' ]

1.  l.append()  

append() function is use to add a single element in a list.

l.append(5)

l=[ 'a', 'b', 'c', 5 ] 

2.  l.extend()

extend() function is use to add multiple elements in a list.

suppose t=[ 'd', 'e', 'f ' ] 

l.extend(t)  

l=[ 'a', 'b', 'c', 5, 'd', 'e', 'f ' ] 

3.  l.insert( <position>, <item> ) 

use to add add single item at specific position.

l.insert(0,"sh")

l=[ 'sh', 'a', 'b', 'c', 5, 'd', 'e', 'f ']  

4.  l.pop() 

use to remove item without using string slice.

there are two ways of writing pop():--

l.pop()    -> remove last element.

l.pop(5)  -> it will remove the sixth element. You'll understand this is you are well familiar with string slicing.

5.  l.remove()

remove the specified item.

l.remove(5)

l=[ 'a', 'b', 'c', 'd', 'e', 'f '] 

6.  l.clear() 

Clear the list. Delets all the elements inside a list.

7.  l.count()

counts the number of elements present in the list, which is given in the column.

l.count('a')

returns -> 1

8.  del l[0]

this method is used for deleting iteming using string slicing.

del l[0]

l=['b', 'c', 'd', 'e', 'f '] 

 

 

 

 

 

 

 

 

 

 

Comments