Instagram
youtube
Facebook

Python Dictionary Interview Questions

Mradul Mishra
Table of Contents

In this blog, we are going to learn about the most important interview questions on Python Dictionaries. Dictionaries are a datatype in python programming language and also one of the most favorite topics for interviewers.

In this blog, we are going to learn about the most important interview questions on Python Dictionaries. Dictionaries is a datatype in python programming language and also one of the most favorite topics for interviewers. We have shortlisted some of the most important dictionary interview questions which will help you python interviews.

 

  1. What are dictonaries in Python?
    Dictonaries are the datatypes in python in which the dataitems are arranged in the form of keys and values. It is opposite to sequential datatypes, where the data items were arranged by range.
    #Example of a Dictionary
    
    dict1={"name": "mradul","age": 23,"city": "indore"}
    print(dict1)

     

  2. Is python dictionaries mutable?
    Yes, dictionaries are mutable you can add, update, delete key, value pairs from it.

     
  3. How to print all keys inside Python Dictionary?
    To print all keys inside dictionary you need to call .keys() object inside the dictionary variable.
    dict1 = {"name":"mradul","age":23,"city":"indore"}
    print(dict1.keys())

     

  4. How to print all values inside Python Dictionary?
    To print all values inside dictionary you need to call .values() object inside the dictionary variable.
    dict1 = {"name":"mradul","age":23,"city":"indore"}
    print(dict1.values())

     

  5. Can we use tuple as keys inside python dictionary?
    Yes, tuple can we used as key inside python dictionary, only if it contain only string, number or tuple. If a tuple contains any mutable datatype inside it like list, it can not be used as keys.

     
  6. Can we use lists as keys inside python dictionary?
    No, python list can not be used as keys inside python dictionary, as they are mutable in nature.

     
  7. What is .item() method inside a dictionary?
    .item() is used to arrange the complete dictionary in the form of list with tuples carrying key, value pair.
    dict1 = {"name":"mradul","age":23,"city":"indore"}
    print(dict1.items())
    
    
    #Output
    dict_items([('name', 'mradul'), ('age', 23), ('city', 'indore')])

     

  8. How can we loop a dictionary using dict.items() method?
    To loop a dictionary you can use the following code.
    dict1 = {"name":"mradul","age":23,"city":"indore"}
    
    for k, v in dict1.items():
        print(k, v)

     

  9. What is enumarate function inside a dictionary?
    You can using enumarate function with dictionary to get position index and corresponding index at the same time.
    for i in enumerate(dict1):
        print(i)
    
    #output
    (0, 'name')
    (1, 'age')
    (2, 'city')

     

  10. What is zip function in python dictionary and how can you use it? How can combine two dictionaries together?
    questions = ['name', 'location', 'favorite language']
    answers = ['Codersdaily', 'Indore', 'Python']
    for q, a in zip(questions, answers):
        print('What is your {0}?  It is {1}.'.format(q, a))
    
    What is your name?  It is Codersdaily.
    What is your location?  It is the Indore.
    What is your favorite language?  It is Python.

     

  11. Write a code to create a dictionary using dictionary comprehension?
    >>> {x: x**2 for x in (2, 4, 6)}
    {2: 4, 4: 16, 6: 36}

     

  12. What does .pop() method do in dictionary?
    Pop inside dictionary can be used to remove the last item as well as any specific item. While in list the pop was just used to remove the last element.
    dict1={"name": "mradul","age": 23,"city": "indore"}
    print(dict1)
    dict1.pop("city")
    print(dict1)
    
    #Output
    {'name': 'mradul', 'age': 23, 'city': 'indore'}
    {'name': 'mradul', 'age': 23}

     

Add a comment:

Comments:

Yash kathole

My interview is next month please help me this is my first interview so little bit hesitation and nervousnes

Teja R

Notification