List Manipulation in Python
Syllabus of “List Manipulation” for Computer Science Students according to CBSE Curriculum
Lists: introduction, indexing, list operations (concatenation, repetition, membership & slicing),
traversing a list using loops, built-in functions: len(), list(), append(), extend(), insert(), count(),
index(), remove(), pop(), reverse(), sort(), sorted(), min(), max(), sum(); nested lists, suggested
programs: finding the maximum, minimum, mean of numeric values stored in a list; linear search
on list of numbers and counting the frequency of elements in a list.
Syllabus of ” List Manipulation ” for Informatics Practices Students according to CBSE Curriculum
Lists: list operations – creating, initializing, traversing and manipulating lists, list methods and
built-in functions.
List in Python Important Notes
Introduction to List :
A list is an ordered sequence which is mutable and made up of one or more elements. A list can have elements of different data types, such as integer, float, string, tuple or even another list. Elements of a list are enclosed in square brackets and are separated by comma.
Examples of List :
Example 1 : L1 is the list of first five odd numbers
>>>L1 = [1, 3, 5, 7, 9]
>>>print(L1)
>>>[1, 3, 5, 7, 9]
Example 2 : L2 is the list of vowels
>>>L2 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
>>>print(L2)
>>>[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Example 3 : L3 is the list of first five multiples of 7
>>>L3 = [7, 14, 21, 28, 35]
>>>print(L3)
>>>[7, 14, 21, 28, 35]
Example 4 : L4 is the list of mixed data types
>>>L4 = [7, ‘school’, 23.5, 17.5, ‘college’]
>>>print(L4)
>>>[7, ‘school’, 23.5, 17.5, ‘college’]
Example 5 : L5 is the list of lists called nested list
>>>L5 = [[“Aman”, 34], [“Sumit”, 30], [“Ravi”, 29]]
>>>print(L4)
>>>[[“Aman”, 34], [“Sumit”, 30], [“Ravi”, 29]]
List Manipulation in Python
Accessing elements in a List :
The elements of a list are accessed using index number. for example
L1 = [23, 43, 12, 54, 34, 65]
Positive Indexing | 0 | 1 | 2 | 3 | 4 | 5 |
23 | 43 | 12 | 54 | 34 | 65 | |
Negative Indexing | -6 | -5 | -4 | -3 | -2 | -1 |
>>>L[2] return 12
>>>L[0] return 23
>>>L[5] return 65
>>>L[-3] return 54
>>>L[1+3] returns 34
>>>L[9] returns error as index is out of range
Lists are Mutable :
It means that the content of List can be modified after it has been created. for example.
>>>L1 = [34, 23, 67, 2, ,66]
>>>L1[3] = 22 #replacing 2 with 22
>>>L1 returns [34, 23, 67, 22, ,66]
List Operations
The data type list allows manipulation of its contents through various operations such as.
1. Concatenation
Python allows us to join two or more lists using concatenation operator (+) for example
>>>L1 = [11, 22, 33, 44, 55]
>>>L2 = [99, 88, 77]
>>>L1 + L2
[11, 22, 33, 44, 55, 99, 88, 77]
>>>L3 = [‘Red’, ‘Green’, ‘Blue’]
>>>L4 = [‘Cyan’, ‘Magenta’, ‘Yellow’ ,’Black’]
>>>L3 + L4
[‘Red’, ‘Green’, ‘Blue’, ‘Cyan’, ‘Magenta’, ‘Yellow’, ‘Black’]
NOTE : The content of L1, L2, L3 and L4 remain same after concatenation operation.
If we try to concatenate a list with elements of some other data type, TypeError occurs. for example
>>>L1 = [1, 2, 3]
>>>L2 = “hello”
>>>L1 + L2
TypeError: can only concatenate list (not “str”) to list
2. Repetition
In Python, a list can be replicated by using replication operator (*). for example
>>>L1 = [11, 22,33]
>>>L1 * 2
>>>[11, 22, 33, 11, 22, 33] #Elements of list L1 repeated twice
>>>L2 = [“Computer”]
>>>L2 * 3
>>>[“Computer”, “Computer”, “Computer”] #Elements of list L2 repeated three times.
3. Membership :
In Python list membership operator return “True” if an element is present in the list, else return “False”. for example.
>>>L1 = [‘a’, ‘b’, ‘c’, ‘d’]
>>> ‘a’ in L1
>>> True
>>> ‘f’ in L1
>>> False
The not in operator returns True if the element is not present in the list, else it returns False.
>>> ‘a’ not in L1
>>> False
>>> ‘f’ not in L1
>>> True
List Manipulation in Python
4. List Slicing :
Slicing is used to extract a portion/slice of list from the existing list. for example
If L is a list, the expression L [ start : stop : step ] returns the portion of the list from index start to index stop, at a step size step
>>> L1 = [‘Ram’, ‘Gyan’, ‘Brij’, ‘Ravi’, ‘Manav’, ‘Sumit’, ‘Raman’]
>>>L1[1 : 4]
[‘Gyan’, ‘Brij’, ‘Ravi’]
>>>L1[2 : 5]
[‘Brij’, ‘Ravi’, ‘Manav’]
>>>L1[3 : 9]
>>>[‘Ravi’, ‘Manav’, ‘Sumit’, ‘Raman’] #L1 will show all elements till end as second index is out of range
>>>L1[3 : 1] #First index is greater than second so return an empty list.
>>>[ ]
>>>L1[ : 3] #as first index is missing so start from 0
>>>[‘Ram’, ‘Gyan’, ‘Brij’]
>>>L1[0 : 6 : 2] #return with step size of 2
>>>[‘Ram’, ‘Brij’, ‘Manav’]
>>>L1[-6 : -3] # return item from index -6 to -3(not included)
>>>[‘Gyan’, ‘Brij’, ‘Ravi’]
>>>L1[: : 2] #return complete list with step size 2 as start ad stop index is missing.
>>>[‘Ram’, ‘Brij’, ‘Manav’, ‘Raman’]
5. Nested List :
A list inside another list is called Nested List. for example
L1 = [1, 4, 5, 2, 12, [7, 8, 9], 11] #sixth element of list is also a list
>>>L1[5]
[7, 8, 9]
To access the element of the nested list of L1, we have to specify two indices L1[m][n]. The first index m will take us to the desired nested list and second index n will take us to the desired element in that nested list. for example
>>>L1[5][1] #index 5 will return the 6thelement of L1 which itself a list and index 1 return 2nd element of that list which is 8.
6. Copying List : (not for IP students)
The simplest way to make a copy of the list is to assign it to another list. for example
>>>L1 = [1, 2, 3]
>>>L2 = L1
>>>L2
>>>[1, 2, 3]
The statement L2 = L1 does not create a new list. Rather, it just makes L1 and L2 refer to the same list object. It means that L2 actually becomes an alias of L1. So, any changes made to either of them will be reflected in the other list. for example
>>>L2[1] = 6
>>>L1
[1, 6, 3]
We can also create a copy or clone of the list as a distinct object by three methods.
Method 1 :
We can slice our original list and store it into a new variable. for example
>>>L1 = [3, 6, 9]
>>>L2 = L1[ : ]
>>>L2
>>>[3, 6, 9]
Here L2 and L1 are two distinct copies of list. Changes done in L1 will not reflect in L2
List Manipulation in Python
Method 2 :
We can use the built-in function list() to make distinct copy of a list. for example
>>>L1 = [3, 6, 9]
>>>L2 = list(L1)
>>>L2
>>>[3, 6, 9]
Method 3 :
We can also use copy() function to make a distinct copy of list. for example
>>>import copy
>>>L1 = [3, 6, 9]
>>>L2 = copy.copy(L1)
>>>L2
>>>[3, 6, 9]
7. Click for Inbuilt Methods of List :
Traversing a List :
We can access each element of the list or traverse a list using a for loop or a while loop.
List Traversal using for loop
>>> L1 = ["Mango", "Banana", "Guava"] >>> for i in L1: print(i) OUTPUT Mango Banana Guava
Another way of accessing the elements of the list is using range() and len() functions: for example
>>> L1 = ["Mango", "Banana", "Guava"] >>> for i in range(len(L1)): print(L1[i]) OUTPUT Mango Banana Guava
List Traversal using while loop
>>> L1 = ["Mango", "Banana", "Guava", "Orange"] >>> i=0 >>>while i<len(L1): print(L1[i]) i = i + 1 OUTPUT Mango Banana Guava Orange
Programs of List Manipulation in Python
Q1. Write a program to accept five numbers from the user and store it in a list.
L1=[] for i in range(5): n1=int(input("Enter any number")) L1.append(n1) print(L1)
Q2. Write a program to accept names of five fruits from the user and store it in a list.
L1=[] for i in range(5): n1=input("Enter any number") L1.append(n1) print(L1)
Q3. Write a program to find the largest or smallest number from the given list. L = [23, 45, 2, 89, 9, 67, 65, 34, 2]
Ans. L = [23, 45, 2, 89, 9, 67, 65, 34, 2] print(max(L)) print(min(L))
Q4. Write a program to find the largest and smallest number from the given list without using inbuilt function (max() and min()). L = [23, 45, 22, 189, 94, 67, 65, 34, 12]
Ans. L = [23, 45, 22, 189, 94, 67, 65, 34, 12] max=L[0] min=L[0] for i in L: if i> max: max=i else: min=i print(max) print(min)
Q5. Write a program to find the average of all the numbers stored in given list L = [5, 10, 15, 20, 25]
Ans. L = [5, 10, 15, 20, 25] print("average is :", sum(L)/len(L)) OUTPUT : average is : 15.0
Programs of List Manipulation in Python
Q6. Write a program to find the average of all the numbers stored in given list L = [5, 10, 15, 20, 25] without using inbuilt function (sum() and len())
Ans. L = [5, 10, 15, 20, 25] sum=0 len=0 for i in L: sum=sum+i len = len +1 print("Average is : ",sum/len)
Q7. Write a program to count the frequency of 1 in the given list. L = [1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 1, 4, 3]
Ans. L = [1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 1, 4, 3] print("Frequency of 1 in list is : ",L.count(1)) OUTPUT : Frequency of 1 in list is : 3
Q8. Write a program to count the frequency of 1 in the given list. L = [1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 1, 4, 3] without using count() function.
Ans. L = [1, 2, 3, 4, 2, 3, 4, 1, 2, 3, 1, 4, 3] count=0 for i in L: if i ==1: count=count+1 print("Frequency of 1 in list is : ",count) OUTPUT Frequency of 1 in list is : 3
Q9. Write a program to increase all the multiples of 5 in the given list by 1. L = [23, 2, 45, 65, 12, 20, 35, 3, 70] for example
Original List = [23, 2, 45, 65, 12, 20, 35, 3, 70]
Modified List = [23, 2, 46, 66, 12, 21, 36, 3, 71]
Ans. L = [23, 2, 45, 65, 12, 20, 35, 3, 70] print("Original list is : ",L) for i in range(len(L)): if L[i] %5==0: L[i]=L[i]+1 print("Modified list is : ",L) OUTPUT : Original list is : [23, 2, 45, 65, 12, 20, 35, 3, 70] Modified list is : [23, 2, 46, 66, 12, 21, 36, 3, 71]
Q10. Write a program to remove the largest element from the list L = [12, 99, 22, 34, 87, 104, 120, 34, 56]
Ans. L = [12, 99, 22, 34, 87, 104, 120, 34, 56] print("Original List is : ", L) L.pop(L.index(max(L))) print("Modified List is : ", L) OUTPUT : Original List is : [12, 99, 22, 34, 87, 104, 120, 34, 56] Modified List is : [12, 99, 22, 34, 87, 104, 34, 56]
List Manipulation in Python
List Manipulation Exercise – 1
Q1. Write the output of the following Print Statements.
L = [8, 9, 0, 7, 6, 5, 6, 4,2]
1. print(L[3])
2. print(L[5])
3. print(L[-4])
4. print(L[0])
5. print(L[-0])
6. print(L[2] ** L[2])
Ans. 1. 7
2. 5
3. 5
4. 8
5. 8
6. 1
Q2. Write the output of the following print statements
L = [23, 34, 12, 65, 43, 25, 36, 89]
- print(L[2 : 2])
- print(L[3 : 7])
- print(L[: : -1])
- print(L[-6 : -1])
Ans.
- [ ]
- [65, 43, 25, 36]
- [89, 36, 25, 43, 65, 12, 34, 23]
- [12, 65, 43, 25, 36]
Q3. Write the output of the following:
L = [[23, 34, 12], [65, 43, 25,], [36, 89]]
- print(L[2 : 2])
- print(L[3 : 7])
- print(L[: : -1])
- print(L[-6 : -1])
Ans.
- [ ]
- [ ]
- [[36, 89], [65, 43, 25], [23, 34, 12]]
- [[36, 89], [65, 43, 25], [23, 34, 12]]
Q4. Write the output of the following :
L = [[2, 3, -1], [‘One’, ‘Two’, ‘Three’], [ 9]]
for i in L: print(i)
Ans. [2, 3, -1] ['One', 'Two', 'Three'] [9]
Q5. Write the output of the following :
L = [[2, 3, -1], ['One', 'Two', 'Three'], [ 9]] for i in L: print( i + i )
Ans. [2, 3, -1, 2, 3, -1] ['One', 'Two', 'Three', 'One', 'Two', 'Three'] [9, 9]
List Manipulation in Python
Q6. Write the output of the following :
L = [['12', '3', '-1'], ['One', 'Two', 'Three'], [ 9]] for i in L: print(i[0]+i[0])
Ans. 1212 OneOne 18
Q7. Write the output of the following :
L = [['12', '3', '-1'], ['One', 'Two', 'Three'], [ 9]] for i in L: print(L.append('four')) print(L)
Ans. It is an infinite loop
Q8. Write the output of following code:
L = [['12', '3', '-1'], ['One', 'Two', 'Three'], [ 9]] for i in range(3): print(L.append('four')) print(L)
Ans. None None None [['12', '3', '-1'], ['One', 'Two', 'Three'], [9], 'four', 'four', 'four']
Q9. Write the statement for doing the following task and also write the final list after performing all the given task.
L = [‘Amit’ , ‘Sumit’, ‘Mini’ , ‘Ronit’ , ‘Abdul’]
- Add name ‘Suman’ after ‘Sumit’
- Add name ‘Mukesh’ at the end.
- Remove the first name of the list .
Ans.
- L.insert(2, ‘Suman’)
- L.append(‘Mukesh’)
- L.pop(0)
Final List will be : [‘Sumit’, ‘Suman’, ‘Mini’, ‘Ronit’, ‘Abdul’, ‘Mukesh’]
Q10. Write the output of the following :
L = [‘Amit’ , ‘Sumit’, ‘Mini’ , ‘Ronit’ , ‘Abdul’]
L.insert(2, (‘Suman’,’Robin’, ‘jack’))
print(L)
Ans. [‘Amit’, ‘Sumit’, (‘Suman’, ‘Robin’, ‘jack’), ‘Mini’, ‘Ronit’, ‘Abdul’]
List Manipulation in Python
Practice Questions of List Indexing and Slicing:
Q1. Write the output of the following:
L1 = [11, 22, 33, 44, 55, 66, 77, 88, 99] 1. print(L1[0]) 2. print(L1[-0]) 3. print(L1[-6]) 4. print(L1[-11]) 5. print(L1[7]) 6. print(L1[3+5]) 7. print(L1[7-2]) 8. print(L1[3 : 8]) 9. print(L1[2 : 6]) 10. print(L1[-8 : -2]) 11. print(L1[7 : 2]) 12. print(L1[-1 : -4]) 13. print(L1[: : 2]) 14. print(L1[2 : 8 : 2]) 15. print(L1[: 5 : 2]) 16. print(L1[1 : 6 : 3]) 17. print(L1[3 : 10 : 2]) 18. print(L1[: : -1]) 19. print(L1[-8 : -3 : 2]) 20. print(L1[7 : 9 : 2])
Answers
1. 11 2. 11 3. 44 4. IndexError: list index out of range 5. 88 6. 99 7. 66 8. [44, 55, 66, 77, 88] 9. [33, 44, 55, 66] 10. [22, 33, 44, 55, 66, 77] 11. [ ] 12. [ ] 13. [11, 33, 55, 77, 99] 14. [33, 55, 77] 15. [11, 33, 55] 16. [22, 55] 17. [44, 66, 88] 18. [99, 88, 77, 66, 55, 44, 33, 22, 11] 19. [22, 44, 66] 20. [88]
List Manipulation in Python
Q2. Write the output of the following
L1 = [11, 22, 33] print(L1+L1) print(L1 * 2) print(L1 * 1) print(L1 * -2) L1.append(23) print(L1) print(sum(L1)) print(min(L1)) print(max(L1)) L1.insert(-1, 'a') print(L1) print(L1.pop()) L1.extend([1, 2, 3]) print(L1) #print(min(L1)) L1.append([4, 5, 6]) print(L1) print(L1.index('a')) L1.remove('a') print(L1) print(L1.pop(4)) print(L1)
[11, 22, 33, 11, 22, 33] [11, 22, 33, 11, 22, 33] [11, 22, 33] [ ] [11, 22, 33, 23] 89 11 33 [11, 22, 33, 'a', 23] 23 [11, 22, 33, 'a', 1, 2, 3] [11, 22, 33, 'a', 1, 2, 3, [4, 5, 6]] 3 [11, 22, 33, 1, 2, 3, [4, 5, 6]] 2 [11, 22, 33, 1, 3, [4, 5, 6]]
Chapter Wise MCQ
2. Flow of Control (Loop and Conditional statement)
3. 140+ MCQ on Introduction to Python
4. 120 MCQ on String in Python
7. 100+ MCQ on Flow of Control in Python
8. 60+ MCQ on Dictionary in Python
Important Links
100 Practice Questions on Python Fundamentals
120+ MySQL Practice Questions
90+ Practice Questions on List
50+ Output based Practice Questions
100 Practice Questions on String
70 Practice Questions on Loops
70 Practice Questions on if-else
Disclaimer : I tried to give you the correct notes of ” List manipulation in Python” , but if you feel that there is/are mistakes in the handouts or explanation of “List manipulation in Python “ given above, you can directly contact me at csiplearninghub@gmail.com.