0% found this document useful (0 votes)
44 views68 pages

Dictionary

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 68

Dictionary

L9,10 and 11
Which of the following option is correct?
1. The keys in a dictionary can be mutable.
2. The values in a dictionary have to be immutable.
3. A dictionary is collection of disordered elements and each element is in the
form a Key and a Value pair.
4. A tuple can be a key, if they only contain strings, numbers and lists.
5. The dict() function is used to construct a dictionary.
A. 1,2,3
B. 3 and 4
C. 3 and 5
D. 4 and 5
Which of the following option is correct?
1. The keys in a dictionary can be mutable.
2. The values in a dictionary have to be immutable.
3. A dictionary is collection of disordered elements and each element is in the
form a Key and a Value pair.
4. A tuple can be a key, if they only contain strings, numbers and lists.
5. The dict() function is used to construct a dictionary.
A. 1,2,3
B. 3 and 4
C. 3 and 5
D. 4 and 5
Which of the following option is correct?
1. The elements in a dictionary can be either keys or values but not both.
2. The indexing operator [] is used to access a key using the value as the parameter inside the
indexing operator.
3. A value in a dictionary can be updated using the indexing operator along with the key.
4. A For loop is used to iterate the elements of a dictionary.
5. Deleting an element in a dictionary only deletes the value but not the key.
A. 1,2,3
B. 3 and 4
C. 4 and 5
D. 2 and 3
Which of the following option is correct?
1. The elements in a dictionary can be either keys or values but not both.
2. The indexing operator [] is used to access a key using the value as the parameter inside the
indexing operator.
3. A value in a dictionary can be updated using the indexing operator along with the key.
4. A For loop is used to iterate the elements of a dictionary.
5. Deleting an element in a dictionary only deletes the value but not the key.
A. 1,2,3
B. 3 and 4
C. 4 and 5
D. 2 and 3
mydict = {"name":"Rama", "branch":"CSE", "place":"HYD"}
print(mydict['name'])
print(mydict['branch'])
print(mydict['place'])
Identify the errors in below program and correct the program:
Solution(Correct Code):
mydict = {"game":"chess","dish":"chicken","place":"home"}
print(mydict.get('game'))
print(mydict['dish'])
print(mydict.get('place'))
mydict['game'] = "cricket"
print(mydict['game'])
Which of the following option is correct?
1. In Python 3 zip() function returns an iterator of tuples true or false?
2. If there is no arguments zip() function returns an empty iterator.
3. While creating dictionaries we need only keys.
4. sorted(dict.items()) function prints the arguments in sorted order.
A. 1,2 and 3
B. 2 and 4
C. 1,2 and 4
D. 3 and 4
Which of the following option is correct?
1. In Python 3 zip() function returns an iterator of tuples true or false?
2. If there is no arguments zip() function returns an empty iterator.
3. While creating dictionaries we need only keys.
4. sorted(dict.items()) function prints the arguments in sorted order.
A. 1,2 and 3
B. 2 and 4
C. 1,2 and 4
D. 3 and 4
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
mydict = dict(zip(list1, list2))
print("list1:", list1)
print("list2:", list2)
print("dictionary:", sorted(mydict.items()))
#Program to create a dictionary from 2 lists
data1 = input("data1: ")
data2 = input("data2: ")
L1 = data1.split(",")
L2 = data2.split(",")
d1 = {}
if(len(L1) == len(L2)):
for i in range(len(L1)):
d1[L1[i]] = L2[i]
print(sorted(d1.items()))
else:
print("length should be equal")
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
key = input("key: ")
if key in dict1:
print("value:", dict1[key])
else:
print("value:", dict1.get(key,"None"))
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
key = input("key: ")
#print("Dictionary:", sorted(dict1.items()))
if key in dict1:
print("True")
else:
print("False")
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
for key, value in sorted(dict1.items()):
print(key, value)
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
for key, value in sorted(dict1.items()):
print(key, '->', value)
Solution:
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))

key = input("key: ")


if key in dict1:
print("value:", dict1.pop(key))
sorted_dict = sorted(dict1.items())
print("dictionary:", sorted_dict)
else:
print("key does not exist")
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
l1 = []
for key in dict1:
l1.append(dict1[key])
l1.sort()
print("max:", l1[-1])
print("min:", l1[0])
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
key = input("key: ")
if key in dict1:
val = input("value: ")
dict1[key] = val
print("sorted dictionary:", sorted(dict1.items()))
else:
print("key does not exist")
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))
print("all(dict1):", all(dict1))
print("any(dict1):", any(dict1))
print("len(dict1):", len(dict1))
print("sorted(dict1):", sorted(dict1))
print("key,value of dictionary: ")
for key in sorted(dict1):
print("%s:%s" %(key, dict1[key]))
#Program to convert keys to values and values to keys
# Note the same value 10 for keys 1 and 5
# First time the entry gets created and the second time the value gets updated
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))

d3 = dict()
for key in dict1:
d3[dict1[key]] = key
print("before exchange:", sorted(dict1.items()))
print("after exchange:", sorted(d3.items()))
troupe = {('Cleese', 'John') : [1, 2, 3],
('Chapman', 'Graham') : [4, 5, 6],
('Idle', 'Eric') : [7, 8, 9],
('Jones', 'Terry') : [10, 11, 12],
('Gilliam', 'Terry') : [13, 14, 15, 16, 17, 18],
('Palin', 'Michael') : [19, 20]}

for last, first in sorted(troupe): # for key in dict


print (first, last, troupe[last, first])
#Program to combine two dictionaries list3 = data3.split(",")
data1 = input("Enter integer elements separated by ,
(comma) for keys of dict1: ")
list4 = data4.split(",")
data2 = input("Enter integer elements separated by , for i in range(len(list3)):
(comma) for values of dict1: ")
list3[i] = int(list3[i])
list1 = data1.split(",")
list2 = data2.split(",")
for i in range(len(list1)): for i in range(len(list4)):
list1[i] = int(list1[i]) list4[i] = int(list4[i])
dict2 = dict(zip(list3, list4))
for i in range(len(list2)):
list2[i] = int(list2[i])
dict3 = {}
dict1 = dict(zip(list1, list2)) for key in (list(dict1.keys()) + list(dict2.keys())):
dict3[key] = dict1.get(key, 0) +
data3 = input("Enter integer elements separated by , dict2.get(key, 0)
(comma) for keys of dict2: ")
data4 = input("Enter integer elements separated by , print(sorted(dict3.items()))
(comma) for values of dict2: ")
#Program to create dictionary of items and occurences
seq = input("seq: ")
list1 = seq.split(",")
for i in range(len(list1)):
list1[i] = int(list1[i])
tuple1 = tuple(list1)
d = dict()
for element in tuple1:
if element not in d:
d[element] = 1
else:
d[element] += 1
print("sorted dictionary:", sorted(d.items()))
#Program to check the existence of a key in 2 dictionaries
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))

data3 = input("data3: ")


data4 = input("data4: ")
list3 = data3.split(",")
list4 = data4.split(",")
dict2 = dict(zip(list3, list4))

kv = input("key: ")
if(kv in dict1.keys() and kv in dict2.keys()):
print("key present in both dictionaries")
elif(kv in dict1.keys()):
print("key present in first dictionary")
elif(kv in dict2.keys()):
print("key present in second dictionary")
else:
print("key is not present")
a = input("data1: ")
b = input("data2: ")
a = a.split(',')
b = b.split(',')
d = dict(zip(a,b))
print("dictionary with key order")
for key,value in sorted(d.items()):
print(key,value)
f = dict(zip(b,a))
print("dictionary with value order")
for key,value in sorted(f.items()):
print(key,value)
data1 = input("data1: ") for i in range(len(list3)):
data2 = input("data2: ")
list3[i] = int(list3[i])
list1 = data1.split(",")
list2 = data2.split(",") for i in range(len(list4)):
list4[i] = int(list4[i])
dict2 = dict(zip(list3, list4))
for i in range(len(list1)):
list1[i] = int(list1[i])
for i in range(len(list2)): dicr = {}
list2[i] = int(list2[i]) for key in (list(dict1.keys()) +
dict1 = dict(zip(list1, list2)) list(dict2.keys())):
x = dict1.get(key, 0)
data3 = input("data3: ")
y = dict2.get(key, 0)
data4 = input("data4: ")
list3 = data3.split(",")
dicr[key] = x + y
list4 = data4.split(",") print("concatenation:", sorted(dicr.items()))
#Program to combine common items of two for i in range(len(list3)):
dictionaries
list3[i] = int(list3[i])
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",") for i in range(len(list4)):
list2 = data2.split(",") list4[i] = int(list4[i])
for i in range(len(list1)): dict2 = dict(zip(list3, list4))
list1[i] = int(list1[i])

for i in range(len(list2)): dict3 = {}


list2[i] = int(list2[i]) keys1 = list(dict1.keys())
dict1 = dict(zip(list1, list2)) keys2 = list(dict2.keys())
for x, y in [(x, y) for x in keys1 for y in keys2]:
data3 = input("data3: ")
if x == y:
data4 = input("data4: ")
list3 = data3.split(",")
dict3[x] = dict1[x] + dict2[y]
list4 = data4.split(",") print("common keys:", sorted(dict3.items()))
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dict1 = dict(zip(list1, list2))

data3 = input("data3: ")


data4 = input("data4: ")
list3 = data3.split(",")
list4 = data4.split(",")
dict2 = dict(zip(list3, list4))

print("sorted dictionary:", sorted(dict1.items()))


copy_dict = dict1.copy()
print("copy of sorted dictionary:", sorted(copy_dict.items()))
print("sorted keys of dictionary:", sorted(dict1.keys()))
print("sorted values of dictionary:", sorted(dict1.values()))
dict1.update(dict2)
print("sorted dictionary after updation:", sorted(dict1.items()))
#Understand and retype the code below to learn how a dictionary is formed by using lists.
dct = {'1':'apple', '2':'orange', '3':'mango', '4':'banana'}
print("dct_keys = dct.keys()")
dct_keys = dct.keys()
print("dct_values = dct.values()")
dct_values = dct.values()
print("dct_keys_list = list(dct_keys)")
dct_keys_list = list(dct_keys)
print("dct_values_list = list(dct_values)")
dct_values_list = list(dct_values)
print("dct_keys_list =", sorted(dct_keys_list))
print("dct_values_list =", sorted(dct_values_list))
print("Combining both the keys and values list using zip");
print("zip(dct_keys_list, dct_values_list)")
zip_result = zip(dct_keys_list, dct_values_list)
print("Creating a list out of the zip result using list function")
zip_result_list = list(zip_result)
print("zip_result_list =", sorted(zip_result_list))
print("Finally creating the dictionary from the zip result list")
dict2 = dict(zip_result_list)
print("dict2 =", sorted(dict2.items()))
data1 = input("data1: ")
data2 = input("data2: ")
list1 = data1.split(",")
list2 = data2.split(",")
dct = dict(zip(list1, list2))

items_view = sorted(dct.items())
print("elements:", (list(items_view)))
print("sorted keys:", sorted(list(dct.keys())))
print("sorted values:", sorted(list(dct.values())))

You might also like