Assignment on List / Dictionary
1.Write a program to find the sum of all elements of a list
l=[1,2,3]
print sum(l)
2. Write a program to find the maximum and minimum element of a list.
t=[1,2,7,56,89,66,78,0]
print t
print "maximum is:",max(t)
print "minimum is:",min(t)
3.Write a program to find the position of maximum and minimum element’s position in a
list.
l=[2,6,12,3,56,8,9,22]
m=max(l)
print m
print l.index(m)
m1=min(l)
print m1
print l.index(m1)
4.Write a program to print all the elements in a list that are greater than a given value.
l= [21,7,8,90,33,50]
print(l)
s=[x for x in l if x>=20]
print( s)
5.Write a program to print all the common elements of two lists.
l=[2,6,12,3,56,1,5]
l1=[10,2,3,56,123]
print l
print l1
for i in l:
for j in l1:
if i==j:
print i
6. Write a program to reverse a list.
l=[1,2,3,4,5]
l.reverse()
print (l)
7. Write a program to find the second largest and second smallest value from a list.
l1=[10,2,3,56,123]
leng = len(l1)
print l1
print leng
print("Second Largest element is:", l1[leng-2])
print("Second Smallest element is:", l1[1])
8.Write a program to find the average of all elements in a list.
l=[1,2,3,4,5]
l1=len(l)
print "Average=",sum(l)/l1
9.Write a program to to generate random numbers within a given range and store it in a
list.
import random
l=[]
for i in range(1,10):
x=random.randint(10,30)
l.append(x)
print l
10. Write a program to split the even and odd elements into 2 different lists.
l=[1,3,45,7,6,78,55,44,86,52,33]
print (l)
even=[x for x in l if x%2==0]
odd=[x for x in l if x%2!=0]
print ("even=",even)
print ("odd=",odd)
11.Write a program to create a dictionary containing names of competition winner
students as keys and number of their wins as values.
d1={"sunita":3,"ryna":5,"ziva":7}
print dict(d1)
12. Write a program to create a phone directory for all your friends and then print it.
d={'priyanka':8769429955,'iti':5389900689,'sikha':77503654891}
print (d)
13.Marks of three students ‘Suniti’, ‘Ryna’, and ‘Ziva’ in 3 subjects are available in
dictionary.
D1 = {1:40, 2:70, 3:70}
D2 = {1:40, 2:50, 3:60}
D1 = {1:70, 2:80, 3:90}
Output:
Key Value
Ryna {1:40, 2:50, 3:60}
Subject(Key) Marks(Value)
1 40
2 50
3 60
D1 = {1:40, 2:70, 3:70}
D2 = {1:40, 2:50, 3:60}
D3 = {1:70, 2:80, 3:90}
D4 = {'Sunita':D1,'Ryna':D2,'Ziva':D3}
print D4
print "NAME MARKS"
for i in D4:
print i," ",D4[i]
print "Ryna:"
print "subject key marks"
for j in D1:
print j," ",D1[j]
print "Sunita:"
print "subject key marks"
for j in D2:
print j," ",D1[j]
print "Ziva:"
print "subject key marks"
for j in D2:
print j," ",D3[j]
14 Create a dictionary whose keys are month names and whose values are the number
of days in the corresponding months.
a) Ask the user to enter a month name and use the dictionary to tell them how
many days are in the month.
b) Print out all of the keys in alphabetical order.
c) Print out the (key value) pairs started by the number of days in each month.
Calendar2018 = {'january' : 31,'february' : 28,'march' : 31,'april' : 30,'may' : 31,'june' : 30,'july' : 31,'august' : 31,
'september' : 30,'october' : 31,'november' : 30,'december' : 31}
monthName=raw_input("Please enter a month name: ")
monthName=monthName.lower()
if monthName in Calendar2018:
print'\nThe month ',monthName,' has ',Calendar2018[monthName],'days\n'
else:
print"the month doesnt exist"
sortedkeys = sorted(Calendar2018)
print'\nThe months in alphabetical order:\n'
for i in sortedkeys:
print i
sortedByValues=sorted(Calendar2018.items(), key=lambda x: x[1])
print"\nThe (key-value) pairs sorted by the number of days in each month are as under.\n"
for i in range(len(sortedByValues)):
print sortedByValues[i]