Skip to content

Latest commit

 

History

History
102 lines (101 loc) · 1.91 KB

48. Unit 4 - Lesson 9.md

File metadata and controls

102 lines (101 loc) · 1.91 KB

Introduction to Dictionaries

Q1. 3,5
Q2. 3,4

Basic Dictionary Operations

Q1.

mydict = {"name":"Rama", "branch":"CSE", "place":"HYD"}
print(mydict["name"])
print(mydict["branch"])
print(mydict["place"])
# Print the values of dictionary using keys

Q2.

mydict = {"game":"chess","dish":"chicken","place":"home"}
print(mydict['game'])
print(mydict['dish'])
print(mydict['place'])
mydict['game'] = "cricket" # change game chess to cricket using respective key
print(mydict['game'])

Q3. 1,2,4
Q4.

# Write your code here
a=input("data1: ").split(",")
b=input("data2: ").split(",")
print("list1:",a)
print("list2:",b)
dict1=dict(zip(a,b))
print("dictionary:",sorted(dict1.items()))

Q5.

a=input('data1: ').split(",")
b=input('data2: ').split(",")
if len(a)==len(b):
	x=dict(zip(a,b))
	print(sorted(x.items()))
else:
	print("length should be equal")

Q6.

list1=input("data1: ").split(',')
list2=input("data2: ").split(',')
mydict=dict(zip(list1,list2))
key=input("key: ")
print("value:",mydict.get(key))

Q7.

a=input("data1: ").split(",")
b=input("data2: ").split(",")
k=input("key: ")
c=dict(zip(a,b))
print(k in c)

Q8.

data1=input("data1: ").split(',')
data2=input("data2: ").split(',')
d=dict(zip(data1,data2))
for x,y in sorted(d.items()):
	print(x,y)

Q9.

data1=input("data1: ").split(',')
data2=input("data2: ").split(',')
d=dict(zip(data1,data2))
for i,j in sorted(d.items()):
	print(i,"->",j)

Q10.

a=input("data1: ").split(",")
b=input("data2: ").split(",")
k=input("key: ")
c=dict(zip(a,b))
if k in c:
	print("value:",c[k])
	del c[k]
	print("dictionary:",sorted(c.items()))
else:
	print("key does not exist")

Q11.

data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list2, list1))
dict1=sorted(dict1)
print("max:",max(dict1))
print("min:",min(dict1))

Q12. Need Solution