Class XI Python List
Class XI Python List
>>> len(list1)
>>> list1
[]
>>> list1
append() Appends a single element passed as an argument at the end of the list A list can
also be appended as an element to an existing list
>>> list1 = [10,20,30,40]
>>> list1.append(50)
>>> list1
>>> list1.append([50,60])
>>> list1
extend() Appends each element of the list passed as argument at the end of the given list
>>> list1 = [10,20,30]
>>> list1.extend(list2)
>>> list1
[10, 20, 30, 40, 50]
>>> list1.insert(2,25)
>>> list1
>>> list1.insert(0,100)
>>> list1
Working with Lists and Dictionaries 61 count() Returns the number of times a given
element appears in the list
>>> list1 = [10,20,30,10,40,10]
>>> list1.count(10)
>>> list1.count(90)
find() Returns index of the first occurrence of the element in the list. If the element is not
present, ValueError is generated
>>> list1 = [10,20,30,20,40,10]
>>> list1.index(20)
>>> list1.index(90)
remove() Removes the given element from the list. If the element is present multiple
times, only the first occurrence is removed. If the element is not present, then ValueError
is generated
>>> list1 = [10,20,30,40,50,30]
>>> list1.remove(30)
>>> list1
>>> list1.remove(90)
ValueError: list.remove(x): x not in list
pop() Returns the element whose index is passed as argument to this function
and also removes it from the list. If no argument is given, then it returns and
removes the last element of the list
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop(3)
40
>>> list1
>>> list1.pop()
60
>>> list1
>>> list1.reverse()
>>> list1
Ex:
>>> list1.reverse()
>>> list1
>>> list1.sort()
>>> list1
>>>list1
[99,89,66,34,28,12]
sorted() It takes a list as parameter and creates a new list consisting of the same elements
but arranged in ascending order
>>>list1 = [23,45,11,67,85,56]
>>> list1
>>> list2
>>> min(list1)
12
>>> max(list1)
92
>>> sum(list1)
284