Chapter 7
List Manipulation
By
Pankaj Singh
PGT (CS)
KV Bharatpur
Computer Science – Class XI
TOPICS
Various List Function, Syntax and their use
– List.append()
– List.index()
– List.extend()
– List.insert()
– List.pop()
– List.count()
– List.reverse()
– List.sort()
– List.clear() Computer Science – Class XI
append(item)
append(item) is used to append any item in
the list.
Example:
>>> L1=[1,2,3,4]
>>> L1
[1, 2, 3, 4]
>>> L1.append(5)
>>> L1
[1, 2, 3, 4, 5]
Computer Science – Class XI
index(item)
index(item) is used to print index number of
any element available in the list.
Example:
>>> L1=[1,2,3,4,5]
>>> print (L1.index(2))
1
>>> print (L1.index(45))
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print (L1.index(45))
ValueError: 45 is not in list Computer Science – Class XI
extend(item)
extend(item/list) is used to append any item
or list in the given list.
Example:
>>> L1=[1,2,3]
>>> L1
[1, 2, 3]
>>> L1.extend([2,3,4,5])
>>> L1
[1, 2, 3, 2, 3, 4, 5]
Computer Science – Class XI
insert(position, item)
insert (position, item ) is used to insert given
item at given index number in the list.
Example:
>>> L1=[11,22,33,44]
>>> L1
[11, 22, 33, 44]
>>> L1.insert(2,100)
>>> L1
[11, 22, 100, 33, 44]
Computer Science – Class XI
pop() / pop(indexNumber)
pop () is used to extract item from the list.
Example:
>>> L1=[1,2,3,4,5]
>>> L1.pop()
5
>>> L1
[1, 2, 3, 4]
>>> L1.pop(1)
2
>>> L1
[1, 3, 4]
Computer Science – Class XI
count (item)
count (item) is used to count the given item
in the list.
Example:
L1=[1,2,3,2,3,1,2,1,3,1,3,2]
>>> L1
[1, 2, 3, 2, 3, 1, 2, 1, 3, 1, 3, 2]
>>> L1.count(2)
4
>>> L1.count(20)
0 Computer Science – Class XI
reverse ()
reverse () is used to reverse the list item.
Example:
>>> L1=[1,2,3,4,5]
>>> L1
[1, 2, 3, 4, 5]
>>> L1.reverse()
>>> L1
[5, 4, 3, 2, 1]
Computer Science – Class XI
sort ()
sort () is used sort the given list.
Example:
>>> L1=[15,23,6,26,11,8]
>>> L1.sort()
>>> L1
[6, 8, 11, 15, 23, 26]
>>> L1.sort(reverse=True)
>>> L1
[26, 23, 15, 11, 8, 6]
Computer Science – Class XI
sort ()
Example on strings:
>>> L1=["pankaj","gagan","amit","alankar","dinesh"]
>>> L1
['pankaj', 'gagan', 'amit', 'alankar', 'dinesh']
>>> L1.sort()
>>> L1
['alankar', 'amit', 'dinesh', 'gagan', 'pankaj']
Computer Science – Class XI
clear()
clear () is used clear the given list.
Example:
>>> L1=[15,23,6,26,11,8]
>>> L1
[15, 23, 6, 26, 11, 8]
>>> L1.clear()
>>> L1
[]
Computer Science – Class XI
Programming Fun. . .
Q1. Initialize a list of 5 names and print the
sorted list in reverse order.
Q2. Initialize a list of names of 3 girls and
another list of names of 4 boys. Merge them
in new list and print all the names
alphabetically.
Computer Science – Class XI
Solution . . . (Q1)
>>>L1=["isha","alok","divyansh","harsh","rudra"]
>>> L1.sort(reverse=True)
>>> L1
['rudra', 'isha', 'harsh', 'divyansh', 'alok']
Computer Science – Class XI
Solution . . . (Q2)
>>> L_Girls=["isha","bhavna","kashish"]
>>> L_Boys=["gaurav","ritesh","dipanshu","ankit"]
>>> List=L_Girls+L_Boys
>>> List.sort()
>>> List
['ankit', 'bhavna', 'dipanshu', 'gaurav', 'isha', 'kashish',
'ritesh']
Computer Science – Class XI
Thank you for your time!
16