Page 395 - Computer Science V2.0 Class 11
P. 395
lst.extend(obj) Inserts the elements in obj, passed as an >>>list_l1=['Touchpad',
argument, at the end of the elements of 'Computer', 'Science']
the list. >>>list_l1.extend([35,40])
obj may not necessarily be a list and ['Touchpad', 'Computer',
could also be a tuple, set, or a dictionary. 'Science', 35, 40]
lst.index(elem) Returns the index of the first occurrence >>>list_l1=['Touchpad',
of the element elem in the list. If elem 'Computer', 'Science']
does not appear in the list, it throws a >>>list_l1.index( 'Computer')
ValueError,
1
lst.count(elem ) Returns the number of times an element >>>list_l1=['Touchpad',
elem appears in a list. 'Computer',
'Science','Computer']
>>>list_l1.count('Computer')
2
lst.reverse() Reverses the order of the elements in the >>>list_l1=['Touchpad',
list 'Computer',
'Science','Computer']
>>>list_l1.reverse()
['Computer', 'Science',
'Computer', 'Touchpad']
lst.sort() Arranges the elements of the list in the >>>list_l1=[80,60,70,10]
ascending order. >>>list_l1.sort()
[10, 60, 70, 80]
lst. Searches for the first instance of the >>>list_l1=['Touchpad',
remove(element) element in the list and removes it. 'Computer',
'Science','Computer']
>>>list_l1.remove('Computer')
['Touchpad', 'Science',
'Computer']
lst.pop(index) Removes the element from the specified >>>list_l1=['Touchpad',
index and returns the element removed 'Computer',
from the list. 'Science','Computer']
>>>list_l1.pop(2)
Science
13.6.1 Creating a Sorted List
The function sorted returns a sorted list comprising the elements of the list passed as argument, but without
modifying it.
>>> lst = ['Physics', 'Chemistry', 'Maths', 'Computer Sc.']
>>> sorted(lst)
['Chemistry', 'Computer Sc.', 'Maths', 'Physics']
>>> lst
['Physics', 'Chemistry', 'Maths', 'Computer Sc.']
Lists and Tuples 381

