Page 1
9.1 Introduct Ion to LIst The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
a string which consists of only characters, a list can
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very
useful to group together elements of mixed data types.
Elements of a list are enclosed in square brackets and
are separated by comma. Like string indices, list indices
also start from 0.
Example 9.1
#list1 is the list of six even numbers
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = ['a','e','i','o','u']
>>> print(list2)
['a', 'e', 'i', 'o', 'u']
#list3 is the list of mixed data types
>>> list3 = [100,23.5,'Hello']
>>> print(list3)
[100, 23.5, 'Hello']
#list4 is the list of lists called nested
#list
>>> list4 =[['Physic s',101],['Chemistry',202],
['Maths',303]]
>>> print(list4)
[['Physics', 101], ['Chemistry', 202],
['Maths', 303]]
9.1.1 Accessing Elements in a List
The elements of a list are accessed in the same way as
characters are accessed in a string.
“Measuring programming
progress by lines of code
is like measuring aircraft
building progress by weight.”
–Bill Gates
Chapter 9
Lists
In this chapter
» Introduction to List
» List Operations
» Traversing a List
» List Methods and
Built-in Functions
» Nested Lists
» Copying Lists
» List as Arguments
to Function
» List Manipulation
Ch 9.indd 189 08-Apr-19 12:40:13 PM
2024-25
Page 2
9.1 Introduct Ion to LIst The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
a string which consists of only characters, a list can
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very
useful to group together elements of mixed data types.
Elements of a list are enclosed in square brackets and
are separated by comma. Like string indices, list indices
also start from 0.
Example 9.1
#list1 is the list of six even numbers
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = ['a','e','i','o','u']
>>> print(list2)
['a', 'e', 'i', 'o', 'u']
#list3 is the list of mixed data types
>>> list3 = [100,23.5,'Hello']
>>> print(list3)
[100, 23.5, 'Hello']
#list4 is the list of lists called nested
#list
>>> list4 =[['Physic s',101],['Chemistry',202],
['Maths',303]]
>>> print(list4)
[['Physics', 101], ['Chemistry', 202],
['Maths', 303]]
9.1.1 Accessing Elements in a List
The elements of a list are accessed in the same way as
characters are accessed in a string.
“Measuring programming
progress by lines of code
is like measuring aircraft
building progress by weight.”
–Bill Gates
Chapter 9
Lists
In this chapter
» Introduction to List
» List Operations
» Traversing a List
» List Methods and
Built-in Functions
» Nested Lists
» Copying Lists
» List as Arguments
to Function
» List Manipulation
Ch 9.indd 189 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 190
#initializes a list list1
>>> list1 = [2,4,6,8,10,12]
>>> list1[0] #return first element of list1
2
>>> list1[3] #return fourth element of list1
8
#return error as index is out of range
>>> list1[15]
IndexError: list index out of range
#an expression resulting in an integer index
>>> list1[1+4]
12
>>> list1[-1] #return first element from right
12
#length of the list list1 is assigned to n
>>> n = len(list1)
>>> print(n)
6
#return the last element of the list1
>>> list1[n-1]
12
#return the first element of list1
>>> list1[-n]
2
9.1.2 Lists are Mutable
In Python, lists are mutable. It means that the contents
of the list can be changed after it has been created.
#List list1 of colors
>>> list1 = ['Red','Green','Blue','Orange']
#change/override the fourth element of list1
>>> list1[3] = 'Black'
>>> list1 #print the modified list list1
['Red', 'Green', 'Blue', 'Black']
9.2 LIst o perat Ions The data type list allows manipulation of its contents
through various operations as shown below.
9.2.1 Concatenation
Python allows us to join two or more lists using
concatenation operator depicted by the symbol +.
#list1 is list of first five odd integers
>>> list1 = [1,3,5,7,9]
#list2 is list of first five even integers
>>> list2 = [2,4,6,8,10]
#elements of list1 followed by list2
n otes Ch 9.indd 190 08-Apr-19 12:40:13 PM
2024-25
Page 3
9.1 Introduct Ion to LIst The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
a string which consists of only characters, a list can
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very
useful to group together elements of mixed data types.
Elements of a list are enclosed in square brackets and
are separated by comma. Like string indices, list indices
also start from 0.
Example 9.1
#list1 is the list of six even numbers
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = ['a','e','i','o','u']
>>> print(list2)
['a', 'e', 'i', 'o', 'u']
#list3 is the list of mixed data types
>>> list3 = [100,23.5,'Hello']
>>> print(list3)
[100, 23.5, 'Hello']
#list4 is the list of lists called nested
#list
>>> list4 =[['Physic s',101],['Chemistry',202],
['Maths',303]]
>>> print(list4)
[['Physics', 101], ['Chemistry', 202],
['Maths', 303]]
9.1.1 Accessing Elements in a List
The elements of a list are accessed in the same way as
characters are accessed in a string.
“Measuring programming
progress by lines of code
is like measuring aircraft
building progress by weight.”
–Bill Gates
Chapter 9
Lists
In this chapter
» Introduction to List
» List Operations
» Traversing a List
» List Methods and
Built-in Functions
» Nested Lists
» Copying Lists
» List as Arguments
to Function
» List Manipulation
Ch 9.indd 189 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 190
#initializes a list list1
>>> list1 = [2,4,6,8,10,12]
>>> list1[0] #return first element of list1
2
>>> list1[3] #return fourth element of list1
8
#return error as index is out of range
>>> list1[15]
IndexError: list index out of range
#an expression resulting in an integer index
>>> list1[1+4]
12
>>> list1[-1] #return first element from right
12
#length of the list list1 is assigned to n
>>> n = len(list1)
>>> print(n)
6
#return the last element of the list1
>>> list1[n-1]
12
#return the first element of list1
>>> list1[-n]
2
9.1.2 Lists are Mutable
In Python, lists are mutable. It means that the contents
of the list can be changed after it has been created.
#List list1 of colors
>>> list1 = ['Red','Green','Blue','Orange']
#change/override the fourth element of list1
>>> list1[3] = 'Black'
>>> list1 #print the modified list list1
['Red', 'Green', 'Blue', 'Black']
9.2 LIst o perat Ions The data type list allows manipulation of its contents
through various operations as shown below.
9.2.1 Concatenation
Python allows us to join two or more lists using
concatenation operator depicted by the symbol +.
#list1 is list of first five odd integers
>>> list1 = [1,3,5,7,9]
#list2 is list of first five even integers
>>> list2 = [2,4,6,8,10]
#elements of list1 followed by list2
n otes Ch 9.indd 190 08-Apr-19 12:40:13 PM
2024-25
Lists 191
>>> list1 + list2
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
>>> list3 = ['Red','Green','Blue']
>>> list4 = ['Cyan', 'Magenta', 'Yellow'
,'Black']
>>> list3 + list4
['Red','Green','Blue','Cyan','Magenta',
'Yellow','Black']
Note that, there is no change in ongoing lists, i.e.,
list1, list2, list3, list4 remain the same
after concatenation operation. If we want to merge two
lists, then we should use an assignment statement to
assign the merged list to another list. The concatenation
operator '+’ requires that the operands should be of list
type only. If we try to concatenate a list with elements of
some other data type, TypeError occurs.
>>> list1 = [1,2,3]
>>> str1 = "abc"
>>> list1 + str1
TypeError: can only concatenate list (not
"str") to list
9.2.2 Repetition
Python allows us to replicate a list using repetition
operator depicted by symbol *.
>>> list1 = ['Hello']
#elements of list1 repeated 4 times
>>> list1 * 4
['Hello', 'Hello', 'Hello', 'Hello']
9.2.3 Membership
Like strings, the membership operators in checks if
the element is present in the list and returns True, else
returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Green' in list1
True
>>> 'Cyan' in list1
False
The not in operator returns True if the element is not
present in the list, else it returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Cyan' not in list1
True
>>> 'Green' not in list1
False
n otes Ch 9.indd 191 08-Apr-19 12:40:13 PM
2024-25
Page 4
9.1 Introduct Ion to LIst The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
a string which consists of only characters, a list can
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very
useful to group together elements of mixed data types.
Elements of a list are enclosed in square brackets and
are separated by comma. Like string indices, list indices
also start from 0.
Example 9.1
#list1 is the list of six even numbers
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = ['a','e','i','o','u']
>>> print(list2)
['a', 'e', 'i', 'o', 'u']
#list3 is the list of mixed data types
>>> list3 = [100,23.5,'Hello']
>>> print(list3)
[100, 23.5, 'Hello']
#list4 is the list of lists called nested
#list
>>> list4 =[['Physic s',101],['Chemistry',202],
['Maths',303]]
>>> print(list4)
[['Physics', 101], ['Chemistry', 202],
['Maths', 303]]
9.1.1 Accessing Elements in a List
The elements of a list are accessed in the same way as
characters are accessed in a string.
“Measuring programming
progress by lines of code
is like measuring aircraft
building progress by weight.”
–Bill Gates
Chapter 9
Lists
In this chapter
» Introduction to List
» List Operations
» Traversing a List
» List Methods and
Built-in Functions
» Nested Lists
» Copying Lists
» List as Arguments
to Function
» List Manipulation
Ch 9.indd 189 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 190
#initializes a list list1
>>> list1 = [2,4,6,8,10,12]
>>> list1[0] #return first element of list1
2
>>> list1[3] #return fourth element of list1
8
#return error as index is out of range
>>> list1[15]
IndexError: list index out of range
#an expression resulting in an integer index
>>> list1[1+4]
12
>>> list1[-1] #return first element from right
12
#length of the list list1 is assigned to n
>>> n = len(list1)
>>> print(n)
6
#return the last element of the list1
>>> list1[n-1]
12
#return the first element of list1
>>> list1[-n]
2
9.1.2 Lists are Mutable
In Python, lists are mutable. It means that the contents
of the list can be changed after it has been created.
#List list1 of colors
>>> list1 = ['Red','Green','Blue','Orange']
#change/override the fourth element of list1
>>> list1[3] = 'Black'
>>> list1 #print the modified list list1
['Red', 'Green', 'Blue', 'Black']
9.2 LIst o perat Ions The data type list allows manipulation of its contents
through various operations as shown below.
9.2.1 Concatenation
Python allows us to join two or more lists using
concatenation operator depicted by the symbol +.
#list1 is list of first five odd integers
>>> list1 = [1,3,5,7,9]
#list2 is list of first five even integers
>>> list2 = [2,4,6,8,10]
#elements of list1 followed by list2
n otes Ch 9.indd 190 08-Apr-19 12:40:13 PM
2024-25
Lists 191
>>> list1 + list2
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
>>> list3 = ['Red','Green','Blue']
>>> list4 = ['Cyan', 'Magenta', 'Yellow'
,'Black']
>>> list3 + list4
['Red','Green','Blue','Cyan','Magenta',
'Yellow','Black']
Note that, there is no change in ongoing lists, i.e.,
list1, list2, list3, list4 remain the same
after concatenation operation. If we want to merge two
lists, then we should use an assignment statement to
assign the merged list to another list. The concatenation
operator '+’ requires that the operands should be of list
type only. If we try to concatenate a list with elements of
some other data type, TypeError occurs.
>>> list1 = [1,2,3]
>>> str1 = "abc"
>>> list1 + str1
TypeError: can only concatenate list (not
"str") to list
9.2.2 Repetition
Python allows us to replicate a list using repetition
operator depicted by symbol *.
>>> list1 = ['Hello']
#elements of list1 repeated 4 times
>>> list1 * 4
['Hello', 'Hello', 'Hello', 'Hello']
9.2.3 Membership
Like strings, the membership operators in checks if
the element is present in the list and returns True, else
returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Green' in list1
True
>>> 'Cyan' in list1
False
The not in operator returns True if the element is not
present in the list, else it returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Cyan' not in list1
True
>>> 'Green' not in list1
False
n otes Ch 9.indd 191 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 192
9.2.4 Slicing
Like strings, the slicing operation can also be applied
to lists.
>>> list1 =['Red','Green','Blue','Cyan',
'Magenta','Yellow','Black']
>>> list1[2:6]
['Blue', 'Cyan', 'Magenta', 'Yellow']
#list1 is truncated to the end of the list
>>> list1[2:20] #second index is out of range
['Blue', 'Cyan', 'Magenta', 'Yellow',
'Black']
>>> list1[7:2] #first index > second index
[] #results in an empty list
#return sublist from index 0 to 4
>>> list1[:5] #first index missing
['Red','Green','Blue','Cyan','Magenta']
#slicing with a given step size
>>> list1[0:6:2]
['Red','Blue','Magenta']
#negative indexes
#elements at index -6,-5,-4,-3 are sliced
>>> list1[-6:-2]
['Green','Blue','Cyan','Magenta']
#both first and last index missing
>>> list1[::2] #step size 2 on entire list
['Red','Blue','Magenta','Black']
#negative step size
#whole list in the reverse order
>>> list1[::-1]
['Black','Yellow','Magenta','Cyan','Blue',
'Green','Red']
9.3 t ravers Ing a LIst We can access each element of the list or traverse a list
using a for loop or a while loop.
(A) List Traversal Using for Loop:
>>> list1 = ['Red','Green','Blue','Yellow',
'Black']
n otes Ch 9.indd 192 08-Apr-19 12:40:13 PM
2024-25
Page 5
9.1 Introduct Ion to LIst The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
a string which consists of only characters, a list can
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very
useful to group together elements of mixed data types.
Elements of a list are enclosed in square brackets and
are separated by comma. Like string indices, list indices
also start from 0.
Example 9.1
#list1 is the list of six even numbers
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = ['a','e','i','o','u']
>>> print(list2)
['a', 'e', 'i', 'o', 'u']
#list3 is the list of mixed data types
>>> list3 = [100,23.5,'Hello']
>>> print(list3)
[100, 23.5, 'Hello']
#list4 is the list of lists called nested
#list
>>> list4 =[['Physic s',101],['Chemistry',202],
['Maths',303]]
>>> print(list4)
[['Physics', 101], ['Chemistry', 202],
['Maths', 303]]
9.1.1 Accessing Elements in a List
The elements of a list are accessed in the same way as
characters are accessed in a string.
“Measuring programming
progress by lines of code
is like measuring aircraft
building progress by weight.”
–Bill Gates
Chapter 9
Lists
In this chapter
» Introduction to List
» List Operations
» Traversing a List
» List Methods and
Built-in Functions
» Nested Lists
» Copying Lists
» List as Arguments
to Function
» List Manipulation
Ch 9.indd 189 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 190
#initializes a list list1
>>> list1 = [2,4,6,8,10,12]
>>> list1[0] #return first element of list1
2
>>> list1[3] #return fourth element of list1
8
#return error as index is out of range
>>> list1[15]
IndexError: list index out of range
#an expression resulting in an integer index
>>> list1[1+4]
12
>>> list1[-1] #return first element from right
12
#length of the list list1 is assigned to n
>>> n = len(list1)
>>> print(n)
6
#return the last element of the list1
>>> list1[n-1]
12
#return the first element of list1
>>> list1[-n]
2
9.1.2 Lists are Mutable
In Python, lists are mutable. It means that the contents
of the list can be changed after it has been created.
#List list1 of colors
>>> list1 = ['Red','Green','Blue','Orange']
#change/override the fourth element of list1
>>> list1[3] = 'Black'
>>> list1 #print the modified list list1
['Red', 'Green', 'Blue', 'Black']
9.2 LIst o perat Ions The data type list allows manipulation of its contents
through various operations as shown below.
9.2.1 Concatenation
Python allows us to join two or more lists using
concatenation operator depicted by the symbol +.
#list1 is list of first five odd integers
>>> list1 = [1,3,5,7,9]
#list2 is list of first five even integers
>>> list2 = [2,4,6,8,10]
#elements of list1 followed by list2
n otes Ch 9.indd 190 08-Apr-19 12:40:13 PM
2024-25
Lists 191
>>> list1 + list2
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
>>> list3 = ['Red','Green','Blue']
>>> list4 = ['Cyan', 'Magenta', 'Yellow'
,'Black']
>>> list3 + list4
['Red','Green','Blue','Cyan','Magenta',
'Yellow','Black']
Note that, there is no change in ongoing lists, i.e.,
list1, list2, list3, list4 remain the same
after concatenation operation. If we want to merge two
lists, then we should use an assignment statement to
assign the merged list to another list. The concatenation
operator '+’ requires that the operands should be of list
type only. If we try to concatenate a list with elements of
some other data type, TypeError occurs.
>>> list1 = [1,2,3]
>>> str1 = "abc"
>>> list1 + str1
TypeError: can only concatenate list (not
"str") to list
9.2.2 Repetition
Python allows us to replicate a list using repetition
operator depicted by symbol *.
>>> list1 = ['Hello']
#elements of list1 repeated 4 times
>>> list1 * 4
['Hello', 'Hello', 'Hello', 'Hello']
9.2.3 Membership
Like strings, the membership operators in checks if
the element is present in the list and returns True, else
returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Green' in list1
True
>>> 'Cyan' in list1
False
The not in operator returns True if the element is not
present in the list, else it returns False.
>>> list1 = ['Red','Green','Blue']
>>> 'Cyan' not in list1
True
>>> 'Green' not in list1
False
n otes Ch 9.indd 191 08-Apr-19 12:40:13 PM
2024-25
Computer SCien Ce – Cla SS xi 192
9.2.4 Slicing
Like strings, the slicing operation can also be applied
to lists.
>>> list1 =['Red','Green','Blue','Cyan',
'Magenta','Yellow','Black']
>>> list1[2:6]
['Blue', 'Cyan', 'Magenta', 'Yellow']
#list1 is truncated to the end of the list
>>> list1[2:20] #second index is out of range
['Blue', 'Cyan', 'Magenta', 'Yellow',
'Black']
>>> list1[7:2] #first index > second index
[] #results in an empty list
#return sublist from index 0 to 4
>>> list1[:5] #first index missing
['Red','Green','Blue','Cyan','Magenta']
#slicing with a given step size
>>> list1[0:6:2]
['Red','Blue','Magenta']
#negative indexes
#elements at index -6,-5,-4,-3 are sliced
>>> list1[-6:-2]
['Green','Blue','Cyan','Magenta']
#both first and last index missing
>>> list1[::2] #step size 2 on entire list
['Red','Blue','Magenta','Black']
#negative step size
#whole list in the reverse order
>>> list1[::-1]
['Black','Yellow','Magenta','Cyan','Blue',
'Green','Red']
9.3 t ravers Ing a LIst We can access each element of the list or traverse a list
using a for loop or a while loop.
(A) List Traversal Using for Loop:
>>> list1 = ['Red','Green','Blue','Yellow',
'Black']
n otes Ch 9.indd 192 08-Apr-19 12:40:13 PM
2024-25
Lists 193
>>> for item in list1:
print(item)
Output:
Red
Green
Blue
Yellow
Black
Another way of accessing the elements of the list is
using range() and len() functions:
>>> for i in range(len(list1)):
print(list1[i])
Output:
Red
Green
Blue
Yellow
Black
(B) List Traversal Using while Loop:
>>> list1 = ['Red','Green','Blue','Yellow',
'Black']
>>> i = 0
>>> while i < len(list1):
print(list1[i])
i += 1
Output:
Red
Green
Blue
Yellow
Black
9.4 LIst Methods and Bu ILt -In Funct Ions The data type list has several built-in methods that
are useful in programming. Some of them are listed in
Table 9.1.
Table 9.1 Built-in functions for list manipulations
Method Description Example
len() Returns the length of the list passed as
the argument
>>> list1 = [10,20,30,40,50]
>>> len(list1)
5
list() Creates an empty list if no argument is
passed
>>> list1 = list()
>>> list1
len(lis t1) returns the
length or total number of
elements of list1.
Ch 9.indd 193 08-Apr-19 12:40:13 PM
2024-25
Read More