Dictionary
Dictionary
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))
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]}
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])
items_view = sorted(dct.items())
print("elements:", (list(items_view)))
print("sorted keys:", sorted(list(dct.keys())))
print("sorted values:", sorted(list(dct.values())))