List
List
XI
Definition
A Python list is a standard data type of Python that can store a sequence of
elements belonging to any type. Python lists are mutable, i.e. you can change the
You can access any element as <listname>[index]. The index can be a forward
index or a backward index.
➢ 0, 1, 2,.... In the forward direction
➢ -1, -2, -3….. In the backward direction
2
XI
Examples of Lists
Examples of Lists are:
3
XI
Indexing
0 1 2 3 4 5
Backward Indexing
4
XI
Indexing
The elements in a list are ordered (numbered), starting from 0.
In case of negative indices, -1 is the index for the last element, -2 for the second
last element, and so on.
5
XI
Creating Lists
To create a list, put a number of values in square brackets.
Example :
6
XI
Creating Lists (Contd.)
A list can be created from a sequence as per the syntax:
7
XI
We can also create a list from single characters or single digits entered via the
9
XI
Accessing elements of List:
● By using the list name and index numbers.
10
XI
Accessing Elements of List:
● By using for loop where the range is specified by the list name.
OR
11
XI
Strings vs Lists
Similarity Difference
12
XI
Operations on Lists
O
l1 = [10,20,30] OUTPUT
Example l2 = [40,50,60]
print("List1 :") List1 :
print(l1) [10,20,30]
print("List2 :") List2 :
print(l2) [40,50,60]
l3 = l1 + l2 [10,20,30,40,50,60] 6
print(l3, len(l3))
14
XI
Replication
The * operator as a replication operator needs two operand i.e a list and an integer.
OUTPUT
l1 = [10,20,30] [10,20,30,10,20,30]
Example print(l1*2)
15
XI
Comparison
All relational and comparison operators (==, >, <, <=, >=, !=) can be used to
compare two lists .
18
XI
Slicing of lists
list1 = [1, 2, 3, 5, 6, 7, 10, 11,12]
21
XI
Solutions :
22
XI
List Functions and Methods
SUM LIST
FUNCTIONS
MIN
LEN
23
XI
List Functions and Methods
1. len()
Syntax : len(listname)
OUTPUT
Length : 5
Example
list1=[10,20,30,40,50]
print("Length:",len(list1))
24
XI
List Functions and Methods
2. max()
Syntax : max(listname)
OUTPUT
Maximum: 78
Example
list1=[1,45,34,11,78]
print("Maximum:",max(list1))
25
XI
List Functions and Methods
3. min()
Syntax : min(listname)
OUTPUT
Minimum: 11
Example
list1=[100,45,34,11,78]
print("Minimum:",min(list1))
26
XI
List Functions and Methods
4. sorted()
OUTPUT
Example
list1=[1,45,34,11,78] Sorted List:
print("Sorted List:") [1,11,34,45,78]
print(sorted(list1))
27
XI
List Functions and Methods
5. sum()
Syntax : sum(<listname>)
OUTPUT
Example
list1=[10,20,30,40,50] Sum:
print("Sum:") 150
print(sum(list1))
28
XI
List Functions and Methods
6. append()
Syntax : listname.append(element)
OUTPUT
Example
List :[1,100,78]
list1=[1,100,78] After appending :
print("List :",list1) [1,100,78,88]
list1.append(88)
print("After appending :")
print(list1)
29
XI
List Functions and Methods
7. extend()
OUTPUT
Example list1=[1,100,78] List :[1,100,78]
list2=[10,11,12] After extending :
print("List :",list1) [1,100,78,10,11,12]
list1.extend(list2)
print("After extending :")
print(list1)
30
XI
List Functions and Methods
8. insert()
Example OUTPUT
list1=[1,100,78]
print("List :",list1) List :[1,100,78]
list1.insert(2,90) After inserting :
print("After inserting :") [1,100,90,78]
print(list1)
31
XI
insert() method
Note : If the pos index is greater than the len(list) (i.e. the index is out of bound),
l1=['a','e','o','u'] OUTPUT
print('List :',l1)
l1.insert(8,'z') List :['a','e','o','u']
print('After inserting') After inserting :
print(l1) ['a','e','o','u','z']
32
XI
insert() method
33
XI
List Functions and Methods
7. pop()
It is used to remove an item from the specified position in the list. If no
Syntax : listname.pop(<index>)
Example OUTPUT
list1=["x","y","z"]
print("List:",list1) List:["x","y","z"]
x=list1.pop(1) After popping :
print("After popping:") ["x","z"]
print(list1)
34
XI
List Functions and Methods
8. remove()
Syntax : listname.remove(<value>)
35
XI
List Functions and Methods
9. clear()
Syntax : listname.clear()
Example OUTPUT
l1=[1,2,3,4,3,2,1] List:[1,2,3,4,3,2,1]
l1.clear() After clearing :
print("After clearing:") [ ]
print(l1)
36
XI
List Functions and Methods
10. count()
Syntax : listname.count(value)
Example OUTPUT
l1=[11,22,33,44,33]
print("List:",l1) List:[11,22,33,44,33]
x=l1.count(33) After counting :
print("After counting:") 2
print(x)
37
XI
List Functions and Methods
11. reverse()
Syntax : listname.reverse()
Example OUTPUT
l1=[4,3,3,2,2,1,1]
print("List1:",l1) List1:[4,3,3,2,2,1,1]
l1.reverse() After reversing :
print("After reversing:") [1,1,2,2,3,3,4]
print(l1)
38
XI
del statement
It also be used to remove individual items/ all items identified by the slice from the
Example OUTPUT
l1=[1,2,3,4,3,2,1]
print("List1:",l1) List:[1,2,3,4,3,2,1]
del l1[4] After deleting :
print("After deleting:") [1,2,3,4,2,1]
print(l1)
39
XI
Program #1
Program to calculate the average of the list of values entered by the user
40
XI
Program #2
Program to find the maximum element from a list of numbers entered by the user
41
XI
Program #3
Program to enter a list of values and a number x to be searched. Search for x in the
42
XI
Program #4
Program to input N numbers from the user and find their sum and average. After that
44
XI
Program #6
Program to input a list, named Pay, from the user and modify each element of Pay,
as per the following rules:
45
XI
Program #6
pay=eval(input("Enter elements of the list:")) Output :
46
XI
Program #7
Program to input a list from the user and modify its content in such a way that the
For example If the content of list P is: 91, 50, 54, 22, 30, 54
Then content of list P should become: 91, 54, 50, 22, 54, 30
47
XI
Program #7
l1=eval(input("Enter elements of the list:")) Output :
48
XI
Program #8
Program to input a list from the user and change all the multiples of 5 in the list to 5
For example, if the list contains: [55, 43, 20, 16, 39, 90, 83, 40, 48, 25]
Then after executing the program, the list content should be changed as follows:
[5, 0, 5, 0, 0, 5, 0, 5, 0, 5]
49
XI
Program #8
l1=eval(input("Enter elements of the list:")) Output :
50
XI
Program #9
Program to input a list of integers and replace each even element of the list with the
For example, if the list is entered as: [100, 43, 20, 56, 32, 91, 80, 40, 45, 21]
After executing the program, the list content should be changed as follows:
51
XI
Program #9
l=eval(input("Enter elements of the list:")) Output :
print("Original List :",l)
Priya
53
XI
Program #11
54
XI
Program #11
l=eval(input("Enter elements of the list:")) Output :
55
XI
Program #12
Write a program to swap the even and odd positions of the values in the list Val.
56
XI
Program #12
l=eval(input("Enter elements of the list:")) Output :
57
XI
Output Questions
Consider the lists A and B given below and write the output of the statements that
follow:
59
XI
Output Questions
Find the output of the following code :
60
XI
Solutions
61
XI
Output Questions
Find the output of the following code :
62
XI
© CS-DEPT DPS MATHURA ROAD
63
2
9
Solutions
XI
Output Questions
Find the output of the following code :
64
XI
Solutions
65
XI
Output Questions
Find the output of the following code :
66
XI
© CS-DEPT DPS MATHURA ROAD
67
234566
Solutions
XI
Output Questions
Find the output of the following code :
68
XI
Solutions
69
XI
Practice Questions
The correct syntax for arranging the items of the list L in descending order is:
Q2. Write a program to input a list and then double the even elements of the list and triple the odd
elements of the list.
Q3. Write a program to input a list containing names of cities and then display the names of all those
cities that start with the alphabet ‘A’.
For example if the list contains [“AHMEDABAD”, CHENNAI”, “NEW DELHI”, “AMRITSAR”,“ AGRA”],
then the program should print AHMEDABAD, AMRITSAR and AGRA.
Q4. Write a program to enter a list and then push all the zeros in the list to the end of the list.
For example: If the list contains [0, 2, 3, 4, 6, 7, 0, 1], then the program should re-arrange the
elements as [2, 3, 4, 6, 7,1, 0, 0]
71
XI