List Comprehensions

List Comprehensions in Python

The "pythonic" way to build lists that are created with simple itteration


What are List Comprehensions?

List Comprehensions are syntactically simple ways to create iterables of modified data. They don't add any new functionality, they just help to simplify common design patterns and cut down on the amount of code used to implement them.
Generally speaking there intended to help simplify common situations where you are creating lists!
For example:

    result = [] # 1. Initialize empty list

# 2. Iterate and store values in the list
for number in range(10): # Square numbers from 0-9 and add them to the result list
    result.append(number**2)

print(result) # 3. Return or use list values
Could be shortened to
    result = [number ** 2 for number in range(10)] # Steps 1-2

print(result) # 3. Return or use list values
It does exactly the same as the above example, it is just shorter. The basic syntax for comprehensions are:
    [operation for variable in iterable]
Where operation is the calculation (or function) being run that results in the output you want (per item), variable is the name for the temporary iteration variable made, and iterable is some form of iterable (list, generator, set etc.).

Real World Applications

The primary purpose of these features in python is to shorten and standardize common design patterns. One problem they both present however is that although convenient to developers who are used to them, they are not intuitive to beginners and so depending on your codebase they may do more harm than good.

Another issue they can present is that the conditions can stack to more than one iterative loop, meaning you can iterate multi-dimensional arrays with this method. If you are at the point of doing this for something more than a 2 dimensional array then, for the sake of your fellow developers just do the long way as if I have to read a list comprehension like the one below you will be off my team:

    # Generates a 3d array that's 3 sublists of 3 elements 10 times
please_no = [[[element for element in range(3)] for num in range(3)] for sublist in range(10)]
print(please_no)"""prints:
[[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]],[[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2],[0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]]"""
You can instead break this out into the longer (but easier to understand):
    result = []
for sublist in range(10): # Create 10 lists with 3 sublists of 3 numbers
    current_sublist= []   # Setup sublist to fill with 3 sub-sublists
    for element in range(3):
        current_sub_sublist = []
        for num in range(3): # Add 3 numbers to sub-sub list
            current_sub_sublist.append(num)
        # append the current sub-sublist of 3 numbers to the sublist
        current_sublist.append(current_sub_sublist)
    # append current sublist of 3 sublists to result
    result.append(current_sublist)

print(result)

Some extra history

List comprehensions largely come from the idea of set theory and set notation.