LeetCampus
Interview Question

What is Dictionary Comprehension? Give an Example.

July 24, 2025
1 view
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Dictionary comprehension is a concise way to create dictionaries in Python. It allows for the generation of a new dictionary from an iterable, applying an expression to each item and optionally filtering items based on conditions. Interviewers ask about dictionary comprehension to assess a candidate's understanding of Python's capabilities for data manipulation, efficiency, and code readability. This technique can significantly reduce the amount of code needed to create complex dictionaries, making it a valuable skill for developers. Understanding this concept is crucial for writing clean, efficient Python code, especially in data processing tasks where transformation of data structures is common. Common misconceptions include confusing dictionary comprehension with list comprehension or not recognizing its performance benefits. It’s important to note that while dictionary comprehension is powerful, it can also lead to less readable code if overused or misused. Therefore, striking a balance between code brevity and clarity is essential.

Sample Answers

Example 1: Basic Dictionary Comprehension

To create a simple dictionary using comprehension, consider the following example:

squares = {x: x**2 for x in range(5)}

In this snippet, we generate a dictionary named squares where the keys are numbers from 0 to 4, and the values are their squares. This results in the dictionary {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}. This method is efficient because it combines the creation and population of the dictionary in a single line of code, enhancing readability and maintainability. When using dictionary comprehension, remember that it’s best suited for scenarios where the transformation of keys and values is straightforward.

Example 2: Dictionary Comprehension with Conditions

You can also incorporate conditions into your dictionary comprehension. For example:

even_squares = {x: x**2 for x in range(10) if x % 2 == 0}

In this case, even_squares will only include squares of even numbers from 0 to 9. The resulting dictionary will be {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}. This showcases the power of comprehension to filter items while constructing the dictionary, which improves both performance and clarity of the code. It's a common use case in data processing where only certain elements need to be transformed and stored.

Example 3: Nested Dictionary Comprehension

Dictionary comprehension can also be nested, allowing for more complex data structures. For instance:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transposed = {i: [row[i] for row in matrix] for i in range(len(matrix[0]))}

This example transposes a 3x3 matrix into a dictionary where each key corresponds to a column index and the value is a list of elements from that column. The resulting dictionary will be {0: [1, 4, 7], 1: [2, 5, 8], 2: [3, 6, 9]}. Nested dictionary comprehensions can be powerful, but they should be used judiciously to avoid complexity that can hinder readability.

Keywords

Pythondictionary comprehensiondata structurescode efficiencyprogramming

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions