Working With Lists and Dictionaries: Back Exercise Question / Answer
Working With Lists and Dictionaries: Back Exercise Question / Answer
Working With Lists and Dictionaries: Back Exercise Question / Answer
a) list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
Ans.: [10,12,26,32,65,80]
b) list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
Ans.: [12,32,65,26,80,10]
c) list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]
Ans.:[10,8,6,4,2]
d) list1 = [1,2,3,4,5]
list1[len(list1)-1]
Ans.: 5
2] Consider the following list myList. What will be the elements of myList after each of the
following operations?
myList = [10,20,30,40]
a) myList.append([50,60])
b) myList.extend([80,90])
myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(myList)):
if i%2 == 0:
print(myList[i])
Ans. So the output will be:
1
3
(a)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)
Ans.: [1,2,3]
(b)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
Ans.: [6,7,8,9,10]
(c)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
Ans.: [2,4,6,8,10]
append() extend()
Example Example
l1=[1,2,3] l1=[1,2,3]
l1.append([4,5]) l1.append([4,5])
print(l1) print(l1)
The output will be: The output will
[1,2,3,[4,5]] be: [1,2,3,4,5]
Ans. a) The * operator treated as a replication operator when the list object is referred to with
any numeric value. So here it will replicate the list values. Therefore it returns [6,7,8,9,6,7,8,9]
b) It is shorthand operator and as explain earlier it will also produce the similar result like
question a).
c) It also produce the similar result as the statement is just like similar.
[7] The record of a student (Name, Roll No, Marks in five subjects and percentage of
marks) is stored in the following list:
stRecord = [‘Raman’,’A-36′,[56,98,99,72,69],78.8]
Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student
Ans .perc=stRecord[3]
print(perc)
b) Marks in the fifth subject.
Ans.marks_sub5 = stRecord[2][4]
print(marks_sub5)
c) Maximum marks of the student
Ans.marks_max = max(stRecord[2])
print(marks_max)
print(rno)
print(sname)
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print(“Maharashtra” in stateCapital)
Ans.So the output will be – True
g) print(stateCapital.get(“Assam”)
h) del stateCapital[“Assam”]
print(stateCapital)
Programming Problems
[1] Write a program to find the number of times an element occurs in the list.
l = []
n = int(input("Enter the no. of elements in the list:"))
cnt=0
for i in range(0,n):
lval=int(input("Enter the value to add in list:"))
l.append(lval)
search_item=int(input("Enter the value to search:"))
for i in range(0,len(l)):
if l[i]==search_item:
cnt+=1
print(search_item, "Found in the list", cnt, " times")
With eval
Answer: Method - I
for n in number:
if max1 < n :
max2 = max1
max1 = n .
max 2 = n
print("Largest 1 : “ , max1)
print("Largest 2 : “ , max2)
Method - II
sorted_list = sorted(1st)
sorted_list [1])
[4] Write a program to read a list of n integers and find their median.
Ans.import statistics as s
lval = eval(input("Enter the values for the list:"))
m = s.median(lval)
print("The median value is:",m)
l = eval(input("Enter the values:"))
l.sort()
len1 =len(l)
mv = len1//2
if len1%2 ==0:
m1, m2 = mv-1, mv
md = (l[m1]+l[m2])/2
else:
md=l[mv]
print("The median value is:",md)
[5] Write a program to read a list of elements. Modify this list so that it does not contain
any duplicate elements i.e. all elements occurring multiple times in the list should appear
only once.
Ans.l = [1,2,3]
list_value=int(input("Enter the value to insert:"))
p=int(input("Enter the position"))
index = p - 1
l.insert(idx,list_value)
print(l)
[7] Write a program to read elements of a list and do the following.
a) The program should ask for the position of the element to be deleted from the list and delete
the element at the desired position in the list.
b) The program should ask for the value of the element to be deleted from the list and delete this
value from the list.
l = [1,2,3]
p=int(input("Enter the position to delete:"))
l.pop(p)
print(l)
b) Delete by value
l = [1,2,3]
value=int(input("Enter the position to delete:"))
l.remove(value)
print(l)
[8] Write a Python program to find the highest 2 values in a dictionary.
d = {11:22,33:46,45:76}
s = sorted(d.values())
print(s[-1],s[-2])
[9] Write a Python program to create a dictionary from a string ‘w3resource’ such that
each individual character mates a key and its index value for fist occurrence males the
corresponding value in the dictionary.
Ans.Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}
string ='w3resource'
dict={}
index = 0
dict[char] = index
index = index + 1
Output is:
10. Write a program to input your friend's, names and their phone numbers and store
them in the dictionary as the key-value pair. Perform the following operations on the
dictionary:
a) Display the Name and Phone number for all your friends.
b) Add a new key-value pair in this dictionary and display the modified dictionary
Ans. phonedict = {}
while True:
elif choice == 1:
print("Name\tPhone Number")
else:
print("Name already exists")
if name in phonedict:
elif choice == 4:
elif choice == 5:
if name in phonedict:
else:
keys = sorted(phonedict)
for k in keys:
print (k, "\t", phonedict[k])
elif choice == 7:
break
else: