Skip to content

Latest commit

 

History

History
88 lines (54 loc) · 2.2 KB

comprehensions.md

File metadata and controls

88 lines (54 loc) · 2.2 KB

Comprehensions

Python comprehensions are a concise, readable syntax for creating lists, sets, dictionaries, and generators. They provide a more compact and intuitive way to process sequences and are often more efficient than using loops. Comprehensions are one of Python's most loved features for their expressiveness and readability.

Types of Comprehensions

List Comprehensions

List comprehensions create lists in a single line of code by iterating over sequences.

Syntax:

[expression for item in iterable if condition] # notice the [...]

Example:

# Squares of even numbers
squares = [x**2 for x in range(10) if x%2 == 0]

Set Comprehensions

Set comprehensions are similar to list comprehensions but produce a set, which means the output will contain unique elements.

Syntax:

{expression for item in iterable if condition} # notice the {...}

Example:

repeated_squares = {x**2 for x in range(-5, 6)}

Dictionary Comprehensions

Dictionary comprehensions are used for creating dictionaries from an iterable.

Syntax:

{key_expression: value_expression for item in iterable if condition} # notice the {key: value ...}

Example:

# Mapping numbers to their squares
square_dict = {x: x**2 for x in range(10)}

Conditional Logic

Comprehensions can incorporate complex conditional logic.

Example:

# Values classified based on their magnitude
classified = ["small" if x < 3 else "large" for x in range(5)]

Comprehensions with Multiple if Conditions

You can include multiple if conditions in a comprehension for more complex filtering.

Example:

# Filter numbers that are even and greater than 5
filtered_numbers = [x for x in range(10) if x % 2 == 0 if x > 5]

Performance Considerations'

While comprehensions are often faster than loops, they should be used judiciously:

  • Readability: Sometimes, for complex operations, a loop might be more readable.

  • Memory Usage: List comprehensions can consume more memory for large lists. In such cases, consider using generator expressions.

  • Debugging: Comprehensions can be harder to debug compared to loops due to their condensed nature.