Python list operation
What is list ?
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
Post a Comment