menu

DEEP DIVE INTO

Python

Topic:comprehensions

menu

List comprehensions, dictionary comprehensions, and set comprehensions are concise and readable ways to create lists, dictionaries, and sets in Python. They provide a more expressive and efficient approach to construct these data structures by applying an expression to each item in an iterable, such as a list or a range. Comprehensions can often replace explicit loops and make the code more compact and readable.

List Comprehensions:

A list comprehension is a concise way to create lists. The basic syntax is:

pythonnew_list = [expression for item in iterable if condition]
  • expression: The operation you want to perform on each item.

  • item: The variable representing each item in the iterable.

  • iterable: The source of data for creating the list.

  • condition (optional): A filter that specifies which items from the iterable to include in the new list.

Examples of list comprehensions:

1. Creating a list of squares of numbers from 1 to 5:

pythonsquares = [x**2 for x in range(1, 6)]
# Output: [1, 4, 9, 16, 25]

2. Creating a list of even numbers from a given list:

pythonnumbers = [1, 2, 3, 4, 5, 6]
evens = [x for x in numbers if x % 2 == 0]
# Output: [2, 4, 6]

Dictionary Comprehensions:

A dictionary comprehension is used to create dictionaries. The basic syntax is:

pythonnew_dict = {key_expression: value_expression for item in iterable if condition}
  • key_expression: The operation to determine the key for each item.

  • value_expression: The operation to determine the value for each item.

  • item: The variable representing each item in the iterable.

  • iterable: The source of data for creating the dictionary.

  • condition (optional): A filter that specifies which items from the iterable to include in the new dictionary.

Examples of dictionary comprehensions:

1. Creating a dictionary of squares for numbers from 1 to 5:

pythonsquares_dict = {x: x**2 for x in range(1, 6)}
# Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

2. Creating a dictionary with even numbers as keys and their squares as values:

pythonnumbers = [1, 2, 3, 4, 5, 6]
evens_dict = {x: x**2 for x in numbers if x % 2 == 0}
# Output: {2: 4, 4: 16, 6: 36}

Set Comprehensions:

Set comprehensions allow you to create sets. The basic syntax is:

pythonnew_set = {expression for item in iterable if condition}
  • expression: The operation to determine each item in the set.

  • item: The variable representing each item in the iterable.

  • iterable: The source of data for creating the set.

  • condition (optional): A filter that specifies which items from the iterable to include in the new set.

Examples of set comprehensions:

1. Creating a set of unique characters from a string:

pythontext = "hello, world"
unique_chars = {char for char in text if char.isalpha()}
# Output: {'o', 'h', 'l', 'r', 'w', 'e', 'd'}

2. Creating a set of even numbers from a list:

pythonnumbers = [1, 2, 3, 4, 5, 6]
evens_set = {x for x in numbers if x % 2 == 0}
# Output: {2, 4, 6}

Nested Comprehensions:

You can also use nested comprehensions to create more complex data structures, such as lists of lists or dictionaries of dictionaries. Here's an example of a list of lists using a nested list comprehension:

pythonmatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [num for row in matrix for num in row]
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Comprehensions are not only concise but also efficient, as they often outperform equivalent code written using traditional loops. They are a powerful feature in Python, enhancing the readability and expressiveness of your code when creating lists, dictionaries, and sets.

1280 x 720 px