An Introduction to the Art of Computer Programming Using Python in the Age of Generative AI

VIII. Comprehensions

Readability counts.
Tim Peters, Zen of Python

Python comprehensions provide a concise and readable way to create lists, dictionaries, and sets by embedding loops and conditional logic within the syntax. These constructs are powerful tools that can improve code readability and expressiveness, allowing you to write more elegant and efficient programs. By using comprehensions, you can perform complex operations in a single line of code, making it easier to understand and maintain.

List Comprehensions

List comprehensions offer a compact and expressive way to create lists. The basic syntax consists of square brackets containing an expression, followed by a for clause. You can extend this with additional for or if clauses to refine the list generation process.


# Basic list comprehension
squares = [x**2 for x in range(10)]
print(f"Squares: {squares}")
        

List comprehensions can also include conditional logic to filter items, creating more targeted results. Notice that when filtering, the if statement goes at the end.


# List comprehension with filtering condition
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(f"Even Squares: {even_squares}")
        

However, if you want to transform values (e.g., "Label A" if true, "Label B" if false), the if-else logic goes at the beginning.


# List comprehension with conditional logic (Ternary Operator)
labels = ["Even" if x % 2 == 0 else "Odd" for x in range(5)]
print(f"Labels: {labels}")
        

Dictionary Comprehensions

Dictionary comprehensions provide a concise way to create dictionaries using a syntax similar to list comprehensions, but with curly braces and key-value pairs. This enables you to build dictionaries in a clear and efficient manner.

In the context of AI, this is frequently used for Tokenization (mapping words to unique IDs).


# Basic Dictionary comprehension
square_dict = {x: x**2 for x in range(5)}
print(f"Squares Map: {square_dict}")

# AI Use Case: Creating a Token Map
vocab = ['apple', 'banana', 'cherry']
token_to_id = {word: i for i, word in enumerate(vocab)}
print(f"Token Map: {token_to_id}")
        

As with lists, dictionary comprehensions can include conditional logic, allowing you to effectively filter and transform data.


# Dictionary comprehension with condition
even_square_dict = {x: x**2 for x in range(10) if x % 2 == 0}
print(f"Filtered Dict: {even_square_dict}")
        

Set Comprehensions

Set comprehensions are similar to list comprehensions but use braces {}. They create sets, which are collections of unique elements that provide an efficient way to handle data without duplicates.


# Set comprehension
# Note: Input has duplicates, but set comprehension removes them automatically
numbers = [1, 2, 2, 3, 3, 3, 4]
unique_squares = {x**2 for x in numbers}
print(f"Unique Set: {unique_squares}")
        
Generative AI Insight: Data Cleaning
Comprehensions are the standard tool for cleaning data before feeding it into AI models. For example, removing stop words, stripping whitespace, or normalizing case is often done in a single line: cleaned_tokens = [word.lower().strip() for word in raw_text if len(word) > 2].

Generator Expressions: Memory Efficiency

When working with massive datasets in AI (e.g., millions of log lines), creating a full list in memory can crash your program. Python offers Generator Expressions, which look like list comprehensions but use parentheses ().

Generators evaluate items "lazily" (one at a time) rather than storing them all at once, saving massive amounts of memory.


import sys

# List Comprehension (Allocates memory for ALL items immediately)
list_comp = [x**2 for x in range(1000)]
print(f"List size in bytes: {sys.getsizeof(list_comp)}")

# Generator Expression (Allocates almost no memory)
gen_exp = (x**2 for x in range(1000))
print(f"Generator size in bytes: {sys.getsizeof(gen_exp)}")

# You can iterate over it just like a list
print(f"First value: {next(gen_exp)}")
        

Nested Comprehensions

Comprehensions can be nested to create complex data structures. For instance, you can use nested list comprehensions to generate a list of lists, which is useful for tasks like creating matrices or multidimensional arrays.


# Nested list comprehension (3x5 Matrix)
matrix = [[j for j in range(5)] for i in range(3)]
print(f"Matrix: {matrix}")
        

Complex Comprehensions

Comprehensions can also perform more advanced operations, like flattening a nested list or combining elements from multiple lists. These techniques can substantially reduce the amount of code needed to handle more intricate data manipulations.


# Flattening a nested list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [item for sublist in nested_list for item in sublist]
print(f"Flattened: {flattened_list}")
        

# Combining elements from multiple lists (Cartesian Product)
list1 = [1, 2]
list2 = ['a', 'b']
combined = [(x, y) for x in list1 for y in list2]
print(f"Combinations: {combined}")
        

Prompting AI for Meaningful Comprehensions

Generative AI can further enhance your ability to write efficient, targeted comprehensions by providing tailored examples or suggestions.

Example Prompt:
Generate a list comprehension in Python that produces a list of the squares of numbers from 1 to 20, but only include the numbers whose squares are divisible by 3.

Resulting AI-generated code:


squares_divisible_by_3 = [x**2 for x in range(1, 21) if (x**2) % 3 == 0]
print(f"Result: {squares_divisible_by_3}")
        
Back to Home