Python Dictionary

In Python, a dictionary is a built-in data structure that stores data in key-value pairs. Dictionaries are mutable, meaning you can change, add, or remove items. Each key in a dictionary must be unique, immutable, and hashable (e.g., strings, numbers, or tuples).

Creating a Dictionary

Dictionaries can be created using curly braces {} with pairs of keys and values separated by a colon :.


dictData = {
    "name": "John",
    "age": 35,
    "city": "London"
}

Alternatively, you can create an empty dictionary and add items later:


dictData = {}
dictData ["name"] = "John"
dictData ["age"] = 35
dictData ["city"] = "London"

You can also use the dict() constructor


my_dict = dict(name="John", age=35, city="London")

Python Dictionary – Interview Questions

Q 1: What is a dictionary in Python?
Ans: A dictionary is an unordered collection of key-value pairs.
Q 2: How do you create a dictionary?
Ans: Using curly braces: my_dict = {'key': 'value'}.
Q 3: Can dictionary keys be duplicated?
Ans: No, keys must be unique.
Q 4: Are dictionary values mutable?
Ans: Yes, values can be changed after creation.
Q 5: How do you access dictionary values?
Ans: Using dict[key] or dict.get(key).

Python Dictionary – Objective Questions (MCQs)

Q1. Which of the following correctly creates a dictionary in Python?






Q2. What type of values can a Python dictionary hold?






Q3. Dictionary elements are accessed using:






Q4. What will be the output of the following code?

d = {'a': 1, 'b': 2, 'c': 3}
print(len(d))






Q5. Which of the following statements is true about dictionaries in Python?






Related Python Dictionary Topics