[go: up one dir, main page]

0% found this document useful (0 votes)
21 views7 pages

Class 9 List Notes

Chapter 9 introduces lists in Python as mutable ordered sequences that can contain elements of different data types. It covers accessing elements, list operations such as concatenation and slicing, and various built-in methods for list manipulation. The chapter also discusses nested lists and how to access their elements using multiple indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Class 9 List Notes

Chapter 9 introduces lists in Python as mutable ordered sequences that can contain elements of different data types. It covers accessing elements, list operations such as concatenation and slicing, and various built-in methods for list manipulation. The chapter also discusses nested lists and how to access their elements using multiple indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 9

Lists

9.1 INTRODUCTION TO LIST “Measuring programming


progress by lines of code
The data type list is an ordered sequence which is
mutable and made up of one or more elements. Unlike
is like measuring aircraft
a string which consists of only characters, a list can building progress by weight.”
have elements of different data types, such as integer,
float, string, tuple or even another list. A list is very –Bill Gates
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'] In this chapter
>>> print(list3)
[100, 23.5, 'Hello']
» Introduction to List
#list4 is the list of lists called nested » List Operations
#list » Traversing a List
>>> list4 =[['Physics',101],['Chemistry',202],
» List Methods and
['Maths',303]]
Built-in Functions
>>> print(list4)
[['Physics', 101], ['Chemistry', 202], » Nested Lists
['Maths', 303]] » Copying Lists
9.1.1 Accessing Elements in a List » List as Arguments
to Function
The elements of a list are accessed in the same way as
» List Manipulation
characters are accessed in a string.

Rationalised 2023-24

Ch 9.indd 189 08-Apr-19 12:40:13 PM


190 COMPUTER SCIENCE – CLASS XI

NOTES #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 OPERATIONS


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

Rationalised 2023-24

Ch 9.indd 190 08-Apr-19 12:40:13 PM


LISTS 191

>>> list1 + list2 NOTES


[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

Rationalised 2023-24

Ch 9.indd 191 08-Apr-19 12:40:13 PM


192 COMPUTER SCIENCE – CLASS XI

NOTES 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 TRAVERSING 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']

Rationalised 2023-24

Ch 9.indd 192 08-Apr-19 12:40:13 PM


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)):
len(lis t1) returns the
print(list1[i]) length or total number of
Output: elements of list1.
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 BUILT-IN FUNCTIONS


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 >>> list1 = [10,20,30,40,50]
the argument >>> len(list1)
5

list() Creates an empty list if no argument is >>> list1 = list()


passed >>> list1

Rationalised 2023-24

Ch 9.indd 193 08-Apr-19 12:40:13 PM


194 COMPUTER SCIENCE – CLASS XI

[ ]
Creates a list if a sequence is passed as >>> str1 = 'aeiou'
an argument >>> list1 = list(str1)
>>> list1
['a', 'e', 'i', 'o', 'u']

append() Appends a single element passed as an >>> list1 = [10,20,30,40]


argument at the end of the list >>> list1.append(50)
>>> list1
The single element can also be a list [10, 20, 30, 40, 50]
>>> list1 = [10,20,30,40]
>>> list1.append([50,60])
>>> list1
[10, 20, 30, 40, [50, 60]]
extend() Appends each element of the list passed >>> list1 = [10,20,30]
as argument to the end of the given list >>> list2 = [40,50]
>>> list1.extend(list2)
>>> list1
[10, 20, 30, 40, 50]
insert() Inserts an element at a particular index >>> list1 = [10,20,30,40,50]
in the list >>> list1.insert(2,25)
>>> list1
[10, 20, 25, 30, 40, 50]
>>> list1.insert(0,5)
>>> list1
[5, 10, 20, 25, 30, 40, 50]
count() Returns the number of times a given >>> list1 = [10,20,30,10,40,10]
element appears in the list >>> list1.count(10)
3
>>> list1.count(90)
0
index() Returns index of the first occurrence of >>> list1 = [10,20,30,20,40,10]
the element in the list. If the element is >>> list1.index(20)
not present, ValueError is generated 1
>>> list1.index(90)
ValueError: 90 is not in list
remove() Removes the given element from the >>> list1 = [10,20,30,40,50,30]
list. If the element is present multiple >>> list1.remove(30)
times, only the first occurrence is >>> list1
removed. If the element is not present, [10, 20, 40, 50, 30]
then ValueError is generated
>>> list1.remove(90)
ValueError:list.remove(x):x not
in list
pop() Returns the element whose index is >>> list1 = [10,20,30,40,50,60]
passed as parameter to this function >>> list1.pop(3)
and also removes it from the list. If no 40
parameter is given, then it returns and >>> list1
removes the last element of the list [10, 20, 30, 50, 60]
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop()
60

Rationalised 2023-24

Ch 9.indd 194 21-May-19 12:28:25 PM


LISTS 195

>>> list1
[10, 20, 30, 40, 50]
reverse() Reverses the order of elements in the >>> list1 = [34,66,12,89,28,99]
given list >>> list1.reverse()
>>> list1
[ 99, 28, 89, 12, 66, 34]

>>> list1 = [ 'Tiger' ,'Zebra' ,


'Lion' , 'Cat' ,'Elephant' ,'Dog']
>>> list1.reverse()
>>> list1
['Dog', 'Elephant', 'Cat',
'Lion', 'Zebra', 'Tiger']
sort() Sorts the elements of the given list >>>list1 = ['Tiger','Zebra','Lion',
in-place 'Cat', 'Elephant' ,'Dog']
>>> list1.sort()
>>> list1
['Cat', 'Dog', 'Elephant', 'Lion',
'Tiger', 'Zebra']

>>> list1 = [34,66,12,89,28,99]


>>> list1.sort(reverse = True)
>>> list1
[99,89,66,34,28,12]
sorted() It takes a list as parameter and creates a >>> list1 = [23,45,11,67,85,56]
new list consisting of the same elements >>> list2 = sorted(list1)
arranged in sorted order >>> list1
[23, 45, 11, 67, 85, 56]
>>> list2
[11, 23, 45, 56, 67, 85]
min() Returns minimum or smallest element >>> list1 = [34,12,63,39,92,44]
of the list >>> min(list1)
12
max() Returns maximum or largest element of >>> max(list1)
the list 92
sum() Returns sum of the elements of the list >>> sum(list1)
284

9.5 NESTED LISTS


When a list appears as an element of another list, it is
called a nested list.
Example 9.2
>>> list1 = [1,2,'a','c',[6,7,8],4,9]
#fifth element of list is also a list
>>> list1[4]
[6, 7, 8]
To access the element of the nested list of list1, we have
to specify two indices list1[i][j]. The first index i
will take us to the desired nested list and second index
j will take us to the desired element in that nested list.

Rationalised 2023-24

Ch 9.indd 195 08-Apr-19 12:40:13 PM

You might also like