PythonTipsPerformance

    Python List Comprehensions: Write Less, Do More

    Jan 18, 20264 min read

    Write cleaner, faster Python code. Replace clunky loops with elegant one-liners using the power of comprehension.

    The Pythonic Way of Filtering#

    If you are coming from Java, C++, or PHP, you are used to writing for loops to process arrays. In Python, there is a better, more "Pythonic" way.

    The Goal: We have a list of numbers. We want a new list containing only the even numbers, with each number multiplied by 2.

    The Old Way (Standard Loop)

    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    result = []
    
    for n in numbers:
        if n % 2 == 0:
            # It is even
            result.append(n * 2)
    
    print(result) 
    # Output: [4, 8, 12, 16, 20]
    

    This works. It is readable. But it takes 5 lines of code and involves manual list appending.

    The Pythonic Way (List Comprehension)

    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    result = [n * 2 for n in numbers if n % 2 == 0]
    
    print(result)
    # Output: [4, 8, 12, 16, 20]
    

    This is 1 line. It reads almost like English: "Give me n times 2, for every n in numbers, if n is even."


    Breakdown of the Syntax#

    A List Comprehension has 3 parts:

    [ expression  for item in iterable  if condition ]
    
    1. Expression (n * 2): What do you want to put in the new list? You can do math here, call functions, or manipulate strings.
    2. Iterable (for n in numbers): What are you looping over? This can be a list, a range, a string, or a file.
    3. Condition (if n % 2 == 0) (Optional): A filter. If this returns False, the item is skipped.

    Real World Examples#

    1. Parsing a list of names

    Clean up user input by stripping whitespace and capitalizing.

    users = ["  alice ", "bob", "  CHARLIE  "]
    
    clean_users = [u.strip().title() for u in users]
    # Result: ['Alice', 'Bob', 'Charlie']
    

    2. Matrix Flattening (Nested loops)

    Say you have a 2D matrix (list of lists) and you want to flatten it into a 1D list.

    matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    
    # The Logic: For every row in matrix... for every num in row... give me num
    flat = [num for row in matrix for num in row]
    # Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    Warning: Don't nest more than 2 levels deep, or your code becomes unreadable. If it's complex, use a normal loop.

    3. Dictionary Comprehensions

    You can do this for Dictionaries (Hash Maps) too!

    Goal: Swap keys and values.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    
    # Syntax: { key: value for ... }
    swapped = {value: key for key, value in my_dict.items()}
    # Result: {1: 'a', 2: 'b', 3: 'c'}
    

    Performance Note#

    List comprehensions are generally faster than for loops in Python. Why? Because the iteration and appending happens inside the underlying C implementation of Python, avoiding the overhead of interpreting Python bytecode for every step of the loop.

    However, they create the entire list in memory. If you are processing 1 billion items, don't use a List Comprehension. It will crash your RAM. Use a Generator Expression instead (replace [] with ()).

    # Creates a generator (lazy iterator) - Uses almost 0 RAM
    huge_gen = (x * 2 for x in range(1000000000))
    

    Try writing your own comprehension

    Ready to try it yourself?

    Experience the power of WebAssembly and Node.js directly in your browser. No setup required.