What will be the output of the following Python code snippet?
a = {}a[1] = 1a['1'] = 2a[1]=a[1]+1count = 0for i in a: count += a[i]print(count)
1
2
4
Error, the keys can’t be a mixture of letters and numbers
Clears all key-value pairs
Updates a specific key
Merges another dictionary into the current one
Removes all values
What will be the output?
a={'a':4,'b':6,'a':7}print(a)
{'a': 5, 'b': 6,'a': 7 }
{'a': 7, 'b': 6}
{'a': 5, 'b': 6 }
Key Error
What will be the output of the following Python code?
d1={"abc":5,"def":6,"ghi":7}print(d1[0])
abc
5
{"abc":5}
error
String
Dictionary
List
Tuple
a={1:"A",2:"B",3:"C"}a.clear() print(a)
None
{ None:None, None:None, None:None}
{1:None, 2:None, 3:None}
{ }
d = {"john":40, "peter":45}print(list(d.keys()))
[“john”, “peter”]
[“john”:40, “peter”:45]
(“john”, “peter”)
(“john”:40, “peter”:45)
a={} a[2]=1 a[1]=[2,3,4] print(a[1][1])
[2,3,4]
3
An exception is thrown
d = {"john":40, "peter":45}print("john" in d)
True
False
Error
The values of a dictionary can be accessed using keys
The keys of a dictionary can be accessed using values
Dictionaries aren’t ordered
Dictionaries are mutable
a={1:"A",2:"B",3:"C"}for i in a: print(i,end=" ")
1 2 3
‘A’ ‘B’ ‘C’
1 ‘A’ 2 ‘B’ 3 ‘C’
Error, it should be: for i in a.items():
a={1:"A",2:"B",3:"C"}print(a.items())
Syntax error
dict_items([(‘A’), (‘B’), (‘C’)])
dict_items([(1,2,3)])
dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
What is the output of the following code?
a = {1:"A", 2: "B", 3: "C"}b = {4: "D", 5: "E"}a.update(b)print(a)
{1:’A’, 2: ‘B’,3: ‘C’}
{1: ‘A’, 2: ‘b’, 3: ‘c’, 4: ‘D’, 5: ‘E’}
{4: ‘D’, 5: ‘E’}
d1 = {"john":40, "peter":45}d2 = {"john":466, "peter":45}print(d1 > d2)
a={1:"A",2:"B",3:"C"}print(a.get(1,4))
A
Invalid syntax for get method
a={1:"A",2:"B",3:"C"}for i,j in a.items(): print(i,j,end=" ")
1 A 2 B 3 C
A B C
1:”A” 2:”B” 3:”C”
a={1:"A",2:"B",3:"C"}b=a.copy()b[2]="D" print(a)
Error, copy() method doesn’t exist for dictionaries
{1: ‘A’, 2: ‘B’, 3: ‘C’}
{1: ‘A’, 2: ‘D’, 3: ‘C’}
“None” is printed
d = {0: 'a', 1: 'b', 2: 'c'}for x in d.keys(): print(d[x])
0 1 2
a b c
0 a 1 b 2 c
none of the mentioned
>>> a={1:"A",2:"B",3:"C"}>>> del a
method del doesn’t exist for the dictionary
del deletes the values in the dictionary
del deletes the entire dictionary
del deletes the keys in the dictionary
What is the output of following code
d = {1:"A", 2:"B", 1:"C"}print(d)
{1:'A',2:'B'}
{1:'C',2:'B'}
{1:'A',1:'C',2:'B'}
d = {0: 'a', 1: 'b', 2: 'c'}for x in d.values(): print(x)
a={1:"A",2:"B",3:"C"}a.setdefault(4,"D") print(a)
{1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
[1,3,6,10]
{1: ‘A’, 2: ‘B’}
dict([[1,”A”],[2,”B”]])
{1,”A”,2”B”}
d = {}d[1] = "Python"d[2] = "Java"print(len(d))
0
dict={"Joey":1, "Rachel":2}dict.update({"Phoebe":2})print(dict)
{'Joey': 1, 'Rachel': 2, 'Phoebe': 2}
{“Joey”:1,”Rachel”:}
{“Joey”:1,”Phoebe”:2}
keys()
getkeys()
allkeys()
keyvalues()
count={} count[(1,2,4)] = 5 count[(4,2,1)] = 7 count[(1,2)] = 6 count[(4,2,1)] = 2 tot = 0 for i in count: tot=tot+count[i] print(len(count)+tot)
25
17
16
Tuples can’t be made keys of a dictionary
More than one key can have the same value
The values of the dictionary can be accessed as dict[key]
Values of a dictionary must be unique
Values of a dictionary can be a mixture of letters and numbers
a = {} a[1] = 1 a['1'] = 2 a[1.0]=4 count = 0 for i in a: count += a[i] print(count) print(a)
6
a={1:"A",2:"B",3:"C"}print(a.get(3))
Error, invalid syntax
C
a={1:"A",2:"B",3:"C"}b={4:"D",5:"E"}a.update(b) print(a)
Method update() doesn’t exist for dictionaries
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
Returns True if any key of the dictionary is true
Returns False if dictionary is empty
Returns True if all keys of the dictionary are true
Method any() doesn’t exist for dictionary
Since “susan” is not a value in the set, Python raises a KeyError exception
It is executed fine and no exception is raised, and it returns None
Since “susan” is not a key in the set, Python raises a KeyError exception
Since “susan” is not a key in the set, Python raises a syntax error
Removes an arbitrary element
Removes all the key-value pairs
Removes the key-value pair for the key given as an argument
Invalid method for dictionary
total={}def insert(items): if items in total: total[items] += 1 else: total[items] = 1insert('Apple')insert('Ball')insert('Apple') print (len(total))
numbers = {}letters = {}comb = {}numbers[1] = 56numbers[3] = 7letters[4] = 'B'comb['Numbers'] = numbers comb['Letters'] = lettersprint(comb)
Error, dictionary in a dictionary can’t exist
‘Numbers’: {1: 56, 3: 7}
{‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
{‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
dictionary()
set()
tuple()
list()
More than one key isn’t allowed
Keys must be immutable
Keys must be integers
When duplicate keys encountered, the last assignment wins
a={'B':5,'A':9,'C':7}print(sorted(a))
[‘A’,’B’,’C’]
[‘B’,’C’,’A’]
[5,7,9]
[9,5,7]
a={1:5,2:3,3:4}a.pop(3) print(a)
{1: 5}
{1: 5, 2: 3}
Error, syntax error for pop() method
{1: 5, 3: 4}
Sequence value pair
Key value pair
Tuple value pair
Record value pair
getkeys ()
key()
test = {1:'A', 2:'B', 3:'C'}test = {} print(len(test))
a=dict()print(a[1])
An exception is thrown since the dictionary is empty
‘ ‘
test = {1:'A', 2:'B', 3:'C'}del test[1]test[1] = 'D'del test[2]print(len(test))
Error as the key-value pair of 1:’A’ is already deleted
d = {}
d = {“john”:40, “peter”:45}
d = {40:”john”, 45:”peter”}
All of the mentioned
d = {0: 'a', 1: 'b', 2: 'c'}for x in d.values(): print(d[x])
d1 = {"john":40, "peter":45}d2 = {"john":466, "peter":45}print(d1 == d2)
d = {0: 'a', 1: 'b', 2: 'c'}for i in d: print(i)