DEV Community

Yashraj Upadhyay
Yashraj Upadhyay

Posted on

Python dictionary interview questions

Python dictionary is one of the widely used Data structure, which gives so much power in the developer's hand. Dictionary related questions are often asked in interviews. Here we discuss very popular questions which are mostly asked by interviewer.

Is python dictionary mutable?
Yes, Python dictionary is mutable as it stores data in key value pair.

How to get the list of all keys and values in python dictionary?
In order to print all the keys, use dict.keys() method and for fetching all the values, use dict.values() method

Can we use tuple and list as the key of dictionary?
Immutable objects can be the key of dictionary, so if tuple contains only string, number or tuple, it can be used as a key of dictionary, while list is immutable datatype so it can't be used a dictionary key

x = {(1,2,3): 'Foo'} #correct
y = {(1,['a', 'b'], 2): 'Bar'} #error
z = {[1,2,3]: 'Foo'} #error
Enter fullscreen mode Exit fullscreen mode

Whats is the use of dict.items() method?
It returns the value in form of list where tuple carry value of key and values

x = {'name': 'abc', 'age': 20}
l = list(x.items()) #output: [('name', 'abc'), ('age', 20)]
Enter fullscreen mode Exit fullscreen mode

How to use enumerate() function with dictionary?
enumerate() methods allow you to iterate over dictionary as well

x = {'name': 'abc', 'age': 20,'salary': 20000}
for key,val in enumerate(x):
    print(key,val)
#output:
0 name
1 age
2 salary
Enter fullscreen mode Exit fullscreen mode

What is the difference between del and pop() method in dictionary
One can simply delete the key from dictionary using del keyword by specifying the name of key. One of the drawback of using del keyword is that it will raise an key error if specified key will not exist in dictionary

x = {'name': 'abc', 'age': 20,'salary': 20000}
del x['name1']
#output
ERROR!
Traceback (most recent call last):
  File "<main.py>", line 2, in <module>
KeyError: 'name1'
Enter fullscreen mode Exit fullscreen mode
del x['name']
#output
{'age': 20, 'salary': 20000}
Enter fullscreen mode Exit fullscreen mode

pop() method allows to remove key value pair by simply passing key name inside pop method. One of the major advantage of using this method is if specified key is not exist, you can specify custom message, plus this method returns value as well which has been removed

x = {'name': 'abc', 'age': 20,'salary': 20000}
print(x.pop('age'))
print(x)
#Output
20
{'name': 'abc', 'salary': 20000}
Enter fullscreen mode Exit fullscreen mode
x = {'name': 'abc', 'age': 20,'salary': 20000}
print(x.pop('age1', 'key not found'))
print(x)
#Output
key not found
{'name': 'abc', 'age': 20, 'salary': 20000}
Enter fullscreen mode Exit fullscreen mode

How to use zip() method with dictionary
zip() method helps you to create a dictionary from two different list

name = ['ajay', 'rohan', 'vikas']
salary = [1000, 2000, 3000]

print(dict(zip(name, salary)))
#output
{'ajay': 1000, 'rohan': 2000, 'vikas': 3000}
Enter fullscreen mode Exit fullscreen mode

*How to use dictionary comprehension *

name = ['ajay', 'rohan', 'vikas']
salary = [1000, 2000, 3000]

d = {key: val for key,val in zip(name, salary) if val > 1000}
print(d)
#output
{'rohan': 2000, 'vikas': 3000}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)