M.E.
S INDIAN SCHOOL, DOHA - QATAR
Note 5 2025- 2026
Section : Boys/Girls Date : 01-10-2025
Class & Div. : XI( All divisions) Subject : Computer Science
Lesson / Topic: LIST MANIPULATIONS IN PYTHON
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LIST MANIPULATIONS IN PYTHON
1.1 LIST: It is a collections of items and each item has its own index value. Index of
first item is 0 and the last item is [Link] n is number of items in a list.
Python list are containers that are used to store a list of values of any type.
Python List are mutable. (I.e. you can change the elements of a list in place.
Python will not create a new list when you make changes to an element of list.)
1.2 Creating a list
Lists are enclosed in square brackets [ ] and each item is separated by a comma.
1.3 Initializing a list
Passing value in list while declaring list is initializing of a list.
e.g. [ ] # empty list
list1 = [‘English', ‘Hindi', 1997, 2000] #list of mixed values type
list2 = [11, 22, 33, 44, 55] # List of numbers
list3 = ["a", "b", "c", "d"] # List of characters
F 061, Rev 01, dtd 10th March 2020 pg. 1
1.4 Blank list creation
A list can be created without element. E.g. List4= [ ]
1.5 Nested list creation
A list can have an element in it, which itself is a list .Such a list is called nested list,
e.g.
L1= [1,”XI C”,”Sanjay”, [‘Anirudh Sanjay’,’Maya Sanjay’] ]
L1 is the nested list with four elements 1,”XI C”,”Sanjay”, [‘Anirudh
Sanjay’,’Maya Sanjay’]
And L1 [3] element is a list [‘Anirudh Sanjay’,’Maya Sanjay’].Length of L1 is 4
and it counts
[‘Anirudh Sanjay’,’Maya Sanjay’] as one element.
1.6 Access Items from a List
List items can be accessed using its index position.
List[0] =80= List[-1]
List[1] =60= List[-2]
List[2] =70= List[-3]
List[3] =85= List[-4]
List 1
List[4] =75= List[-5]
1.7 List is similar to string in the following ways.
Length: Using inbuilt function len () which helps to find the length of the
list.
Indexing and slicing: L[i] returns the item of index i .L [i: j] returns a
new list, containing the objects at indices between I and j -1.
Membership Operators: Both ‘in’ and ‘not in’ operators work on lists
just like they work for other [Link]. ‘in’ tells if the element is
present in the list or not, and ‘not in’ does the opposite.
Concatenation and replication Operators + and *: The + operator join
two list, it add one list at the end of other list. The * operator replicates the
list.
F 061, Rev 01, dtd 10th March 2020 pg. 2
Notice that it changed the element in place (’a’
changed to ‘A’) in the position of [0], no new list is
created because list is mutable.
Notice that the address of the list (in memory)
student is not changed before and after the
updation. It is same, which means List is
MUTABLE.
1.8 TRAVERSING THE LIST:
For loop makes it easy to traverse through the each item in the list.
Syntax: for <item> in <List>:
Process each item here
L=
OUTPUT
80
60
e.g. for a in L: 70
print (a) 85
75
Using ‘in’ Operator inside for loop:
L=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
for a in L:
print (i)
Using range () function inside for loop:
L=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
L=len(L)
for a in range(l):
print(L[a])
F 061, Rev 01, dtd 10th March 2020 pg. 3
1.9 Aliasing
In python variables are referred to objects. The list we create in python are
also objects we are refer to. Since list are mutable .i.e. they can be modified.
If we assign the elements of list to another ,bothe objects will refer to the
same list of items.
This statement will copy the all values from subject to temp.
Bothe temp and subject is pointing to the same
set of values. That is why Address is same.
PROGRAM 1: Write a python program to print the elements in the list
[‘P’,’Y’,’T’,’H’,’O’,’N’]
In separate lines along with elements positive and negative index.
OUTPUT:
1.10 COMPARING THE LIST:
Each element in the list can be compared using standard comparison operator.i.e
<,>, <=,>=, = =,! =.
Python internally compares individual items of the lists in lexicographical order. This
means that to compare equal, each corresponding element must compare equal and the
two sequence must be of the same datatype.
e.g The corresponding elements of the two
sequence should be of comparable
type, otherwise it shows error.
Here corresponding second element of
L1 is 1 and l3 is [2, 3], so integer and
list cannot be comparable both are
different datatypes.
F 061, Rev 01, dtd 10th March 2020 pg. 4
[1,2,8,9] < [9,1] TRUE 1<9 is True
[1,2,8,9] < [1,2,9,1] TRUE 8<9 is True
[1,2,8,9] < [1,2,9,10] TRUE 8<9 is True
[1,2,8,9] < [1,2,8,4] FALSE 9<4 is False
1.11 LIST OPERATIONS:
a) Joining Lists (Concatenation):- We can concatenate two or more lists using ‘+’
concatenation Operator.
e.g l1= [1, 2, 3, 4] l2= [5, 6, 7, 8]
We could not add a
list with an integer
value.
Add list to another list, it will add the second
list to the end of the first list.
b) Replicating Lists (Repetition):- We can ‘*’ operator to replicate a list to a specified
number of times.
c) Slicing of A List: - List elements can be accessed in subparts.
Seq=L[start:stop]
F 061, Rev 01, dtd 10th March 2020 pg. 5
It create a list slice out of the list L with elements falling in between indexes
start and stop, not including stop.
Note: List is supporting slice steps. Seq =L [start: stop: step]
Include every 2 nd element
Include every 3 rd element
No start and stop, only step is given, i.e. from the
entire list pick up every 3 rd. element.
Using slices for list modification:
Modifying L list with 1 and 2
Modifying L list with 0 and 1 position with 1 value “a”
d)Membership Testing
Membership testing is an operation carried out to check whether a particular
element/item is a member of that sequence or not.
e.g.1 Output:
x='Python' True
print ('o' in x)
e.g.2 Output:
y=[10,20,30,40] False
print (50 in y)
e.g.3 Output:
student_XII = ['Ritu', 'Saksham', 'Abha', 'Varun'] Ritu
for name in student_XII: Saksham
print(name) Abha
Varun
F 061, Rev 01, dtd 10th March 2020 pg. 6
e)Indexing
Index is a number specifying the position of an element in a list. It enables
access to individual elements in the list.
e.g.1 Output:
list1=['Red','Green','Blue'] Blue 30
list2=[10,20,30]
print(list1[-1])
print(list2[2])
1.12 DELETING ITEM FROM THE LIST:
del statement is used to remove an individual item or to remove all items
identified by the slice.
Syntax:
del List[<index>]
del List[<start>:<stop> ]
e.g. Output
list=[1,2,3] #List created ('list before delete', [1, 2, 3])
print('list before delete', list) #print ('list after delete', [1, 3])
the list before deleting
del list [1] #deleting the list item
form the index 1
print('list after delete', list) #print the
list after deleting
e.g.
del list[0:2] # delete first two items
del list # delete entire list
1.13 Built in Functions and methods used in List
Python provides several built-in functions to perform various operations on
list.
a) index()
index () function returns the index of first matched item from the list.
Syntax:
List. Index (item)
e.g. Output
L5=[10,20,10,30,10,40] 0
print([Link](10)) 2
x=['USA','UK','INDIA','JAPAN']
print([Link]('INDIA'))
F 061, Rev 01, dtd 10th March 2020 pg. 7
b) append()
append () method adds a single item to the end of the list.
It doesn't create a new list; rather it modifies the original list.
Syntax: [Link] (item)
append () method is used to add an Item to a List.
e.g:1 Output
list=[1,2] ('list before append', [1, 2])
print('list before append', list) ('list after append', [1, 2, 3])
[Link](3)
print('list after append', list)
NOTE: - extend () method can be used to add multiple item at a time in [Link] -
[Link] ([3, 4])
e.g.2 Output
color=['Red','Green','Blue'] ['Red', 'Green', 'Blue', 'White']
[Link]('White')
print(color)
c) extend()
extend () function adds one list at the end of another list.
Syntax:
[Link] (list2)
e.g. Output
L1=[10,20,30,40] [10, 20, 30, 40, 50, 60]
L2=[50,60]
[Link](L2)
print(L1)
d) insert()
insert () function can be used to insert an element at a specified index.
Syntax:
list_name.insert (index number, value)
e.g. Output
names=['Ajay','Vinay','Sonia'] ['Ajay', 'Vinay', 'Sanjay', 'Sonia']
names. Insert(2,'Sanjay')
print(names)
e) pop()
This method is used to remove the item from the list.
Syntax:
list_name.pop (index number)
F 061, Rev 01, dtd 10th March 2020 pg. 8
e.g. Output
l=[1,2,3,4] 1
e=[Link](0) [2,3,4]
print(e)
print(l)
f) remove()
remove () function can be used to remove an element at a specified index.
Syntax: list_name.remove (value)
e.g. Output
names=['Ajay','Vinay','Sonia'] ['Ajay', 'Sonia']
[Link]('Vinay')
print(names)
g) clear()
clear () removes all the items from the list.
Syntax:
[Link] ()
e.g. Output
L1=[10,20,30,40] []
[Link]()
print(L1)
h) count()
count() function counts and returns how many times an element has
occurred in a list.
Syntax:
[Link](element)
e.g. Output
L5=[10,20,10,30,10,40] 3
print([Link](10))
i) reverse()
reverse () function in python reverses the order of the elements in a list. It
replaces a new value ‘in place’ of an item that already exists in the list.
Syntax:
list_name.reverse ()
e.g. Output
L2=['Sam', 'Aman'] ['Aman', 'Sam']
[Link]()
print(L2)
F 061, Rev 01, dtd 10th March 2020 pg. 9
j) sort()
sort () function sorts the items as the list in ascending or descending order.
Syntax:
list_name.sort ()
e.g 6. Output
L1=[10,4,22,3,100,2] [2, 3, 4, 10, 22, 100]
[Link]()
print(L1)
Syntax:
list_name.sort(reverse=True)
#sort in descending order Output
names=['Ajay', 'Vinay', ['Vinay', 'Sonia', 'Sanjay', 'Ajay']
'Sanjay', 'Sonia']
[Link](reverse=True)
print(names)
Functions all together
*******THE END*******
F 061, Rev 01, dtd 10th March 2020 pg. 10