Dictionary Comprehension

Carson West

Dictionaries

Dictionary Comprehension

Dictionary comprehension provides a concise way to create dictionaries in Python. It follows a similar syntax to list comprehension, but creates key-value pairs instead of a list of items.

Basic Syntax:

{key_expression : value_expression for item in iterable if condition} 

Examples:

  1. Simple Dictionary Comprehension:
squares = {x: x*x for x in range(1, 6)} 
print(squares)  # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
  1. Dictionary Comprehension with Conditional Logic:
even_squares = {x: x*x for x in range(1, 11) if x % 2 == 0}
print(even_squares) # Output: {2: 4, 4: 16, 6: 36, 8: 64, 10: 100}
  1. Dictionary Comprehension from Two Iterables (using zip):
keys = ['a', 'b', 'c']]
values = 1, 2, 3
my_dict = {k: v for k, v in zip(keys, values)}
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
  1. Dictionary Comprehension with Nested Loops (for more complex scenarios):
matrix = 1, 2, 3, 4
flattened_dict = { (i, j): matrix[i]][j]] for i in range(len(matrix)) for j in range(len(matrix[0]]))}
print(flattened_dict) # Output: {(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}

List Comprehension (This will be a separate note about list comprehensions)

Iteration in Python (This will be a separate note about various iteration techniques in Python)

Zip Function (This will be a separate note about the zip function)

Dictionary comprehensions offer a powerful and readable way to create dictionaries directly from iterables. They are particularly useful for transforming data and creating dictionaries based on specific conditions. Remember to always prioritize readability and choose the method (comprehension or traditional loop) that best suits the complexity of your task.