List Manipulation - Exercise Questions Check Point 7.1
List Manipulation - Exercise Questions Check Point 7.1
(b) [-2, 1, 3]
L=[-2,1,3] or L= list([-2,1,3])
(e) [0, 1, 2]
L=[0,1,2] or L =list([0,1,2]) or L=list(range(3))
7. If a=[5,4,3,2,1,0] evaluate the following expressions:
(a) a[0]
(b) a[-1]
(c) a[a[0]]
(d) a[a[-1]]
(e) a[a[a[a[2] + 1]]]
8. Can you change an element of a sequence? What if the sequence is a string? What if the sequence is a list?
Ans. If the sequence type is mutable, then the element can be changed otherwise not possible
If the sequence is a string, the element can’t be changed.
If the sequence is a list , the elements can be changed.
9. What does a+b amount to if a and b are lists?
Ans. A new list with all the elements of ‘a’ and ‘b’ combined.
10. What does a*b amount to if a and b are lists?
It will raise an error.
11. What does a+b amount to if a is a list and b=5?
It will raise an error.
12. Is a string same as a list of characters? No
13. Which functions can you use to add elements to a list?
Ans. append( ) , extend( ) , insert( )
14. What is the difference between append( ) and insert( ) methods of list?
Ans. append( ) method inserts an element to the last of the list .
insert( ) method can insert an element at any position.
15. What is the difference between pop( ) and remove( ) methods of list?
Ans. pop( ) function delete and return the element of passed index. Index passing is optional , if not passed ,
element from last will be deleted.
List.pop(<index>)
remove( ) function removes the first occurence of given item from the list.
List.remove(<value>)
16. What is the difference between append( ) and extend( ) methods of list?
Ans. append( ) function adds a single item to the end of the list.
List.append(<item>)
extend( ) function add multiple elements from a list supplied to it as argument.
List.extend(<list>)
17. How does sort( ) work? Give example.
Ans. sort( ) function sorts the items of the list by default in ascending order. It does not create a new list.
List.sort ( )
To sort in descending order
List.sort(reverse =True)
18. What does list.clear( ) function do?
Ans. The clear( ) function removes all elements from the list.
L=[1,2,3]
L.clear( )
print(L)
o/p
[]
SOLVED PROBLEMS
1. How are lists different from strings when both are sequences?
2. What are nested lists?
3. What does each of the following expressions evaluate to?
Suppose that L is the list
L=[“These”, “are”, “a”, [“few”, “words”], “that”, “we”, “will”, “use”]
(a) L[3:4] [[“few”,”words”]]
L[3:4][0] [“few”, “words”]
L[3:4][0][1] “words”
L[3:4][0][1][2] r
Ans: [2,4]
[2,5]
12. Find the errors
L1=[1, 11, 21, 31]
L2 = L1 + 2
L3 = L1 * 2
Idx = L1.index(45)
output
523
[]
<class 'list'> <class 'str'>
PROGRAMS
(1) Program to print elements of a list [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’] in separate lines along with element’s both indexex
(positive and negative)
L = [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’]
length = len(L)
for a in range(length):
print(“At indexes”, a, “and”, (a – length), “element :”, L[a])
(2) i. Program to find minimum element from a list of element along with its index in the list (without using built-in
functions).
Lst = eval(input(“Enter List elements : “))
length = len(Lst)
min_ele = Lst[0]
min_index = 0
for i in range(1, length):
if Lst[i] < min_ele:
min_ele = Lst[i]
min_index = i
print(“Given List is : “, Lst)
print(“The minimum element of the given list is : “, min_ele)
print(“ The minimum element “, min_ele, “is at index “, min_index)
(2)ii. Program to find minimum element from a list of element along with its index in the list (using built-in
functions).
Lst = eval(input(“Enter List elements : “))
Lst.sort( )
print(“The minimum element of the given list is : “, Lst[0])
OR
Lst = eval(input(“Enter List elements : “))
small=min(Lst)
pos=Lst.index(small)
print(“The minimum element of the given list is : “, small)
print(“ The minimum element “, min_ele, “is at index “,pos)
(7) Write a program to find the second largest number of a list of numbers (without using built-in functions).
Lst=eval(input(“Enter List elements :”))
Length=len(Lst)
large=secondlarge=0
if Lst[0] > Lst[1]:
large=Lst[0]
secondlarge=Lst[1]
else:
large=Lst[1]
secondlarge=Lst[0]
for i in range(1, Length):
if Lst[i] > large:
secondlarge=large
large=Lst[i]
elif Lst[i] > secondlarge:
secondlarge=Lst[i]
print(“Largest number of the list =”, large)
print(“Second largest number of the list =”, secondlarge)
(8) i. Write a program that reverses an array of integers (in place) [without using built in function]
Lst=eval(input(“Enter List elements :”))
Length=len(Lst)
rev=-1
mid=Length//2
for i in range(0,mid):
Lst[0], Lst[rev] = Lst[rev], Lst[i]
rev-=1
print(“List after reversing :”,Lst)
(8) ii Write a program that reverses an array of integers (in place) [using built in function]
Lst=eval(input(“Enter List elements :”))
Lst.reverse( )
print(“List after reversing :”,Lst)
(9) Write a program that inputs two lists and creates a third list that contains all elements of the first followed by
all the elements of the second.
Lst1=eval(input(“Enter elements in List 1 :”))
Lst2=eval(input(“Enter elements in List 2:”))
Lst3=Lst1 + Lst2
print(“Resultant List =”, Lst3)
(10) Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the entries in the list
that are greater than 10 with 10.
Lst=eval(input(“Enter List elements (between 1 and 12) :”))
Length=len(Lst)
for i in range(Length):
if Lst[i]>10:
Lst[i]=10
print(“Resultant List =”, Lst)
(11) Write a program that takes two lists L and M of the same size and adds their elements together to form a new
list N whose elements are sums of the corresponding elements in L and M. For instance, if L=[3,1,4] and
M=[1,5,9], then N should equal [4,6,13]