Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

List comprehensions, dictionary comprehensions, and set comprehensions in Python

Tutorial 10.1 - Comprehensions and Generators

List comprehensions, dictionary comprehensions, and set comprehensions.

List comprehensions are a concise and Pythonic way to create lists based on existing Iterable objects (such as lists, strings, or ranges) with a single line of code. They offer a more readable and expressive way to perform common operations on lists compared to traditional for loops. List comprehensions consist of three main components :
  • Output Expression: This is the expression that calculates the value of each element in the new list. It is placed at the beginning of the list comprehension.
  • Input Iterable: This is the iterable (e.g., a list, string, or range) over which the list comprehension iterates. It is placed after the Output expression.
  • Optional Predicate (Condition): You can include an optional condition that filters elements from the input iterable. The condition is placed after the input iterable and is used to determine whether an element should be included in the new list.
Here's the basic syntax of a list comprehension:

new_list = [output_expression for item in input_iterable if condition]

Let's break down the components and provide examples:

Output Expression:

  • This is the value that you want to include in the new list.
  • Example: x * 2 or len(item)

Input Iterable:

  • It defines the source of elements to iterate over.
  • Examples:
    • List: [1, 2, 3, 4, 5]
    • String: "Hello"
    • Range: range(1, 6) (generates numbers 1 through 5)

Optional Predicate (Condition):

  • This is an optional filter that determines whether an item from the input iterable should be included in the new list.
  • Example: item % 2 == 0 (includes only even numbers)
Here are some examples of list comprehensions:

Creating a list of squares from 1 to 10:

squares = [x ** 2 for x in range(1, 11)]
# Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


Filtering even numbers from a list:

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


Converting characters in a string to uppercase:

text = "hello world" uppercase_chars = [char.upper() for char in text] 
# Output: ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D']

Nested list comprehension (creating a matrix):

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

List comprehensions are a powerful and elegant way to manipulate lists and other iterable data structures in Python. They can make your code more concise and easier to read when used appropriately


This post first appeared on Tsarde, please read the originial post: here

Share the post

List comprehensions, dictionary comprehensions, and set comprehensions in Python

×

Subscribe to Tsarde

Get updates delivered right to your inbox!

Thank you for your subscription

×