Python List Comprehension: Three Helpful Tips

Ronald Eddings
Hacker Valley Studio
3 min readJan 16, 2018

List comprehension is a technique used in Python to transform one list (or iterable) to another list. If you’re not using list comprehension then the example below will probably look familiar:

>>> newList = []
>>> for x in range(5):
newList.append(x)
>>> print(newList)
[0, 1, 2, 3, 4]

List comprehension can simplify the above example to the following:

>>> newList = [x for x in range(5)]>>> print(newList)
[0, 1, 2, 3, 4]

With list comprehension on your side, you can save time and several lines of code. I’d like to pass on a few tips that you can leverage while employing list comprehension.

Tip #1 — Conditionals

List comprehension provides us the flexibility of using conditional statements. Below is an example to append the value of x to a list if x is a multiple of 3 while looping through numbers 1–10.

>>> [x for x in range(10) if x % 3 == 0]
[0, 3, 6, 9]

In the above example, the value is added to the list only when the bolded condition is met.

Surprisingly, with list comprehension, we can also use the else statement. In one line, we can solve the classic FizzBuzz interview challenge question.

>>> ['FizzBuzz' if (x % 3 == 0 and x % 5 ==0) else 'Fizz' if (x % 3 == 0) else 'Buzz' if (x % 5 == 0) else x for x in range(1,21)][1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz']

Tip #2 — Nested Loops

If you love to get carried away with list comprehension, be sure to check out nested loops. Let’s observe the following example below and attempt to reach the same solution with list comprehension.

>>> styles = ['chopped','diced','minced']
>>> fruits = ['apples','bananas','oranges']
>>> plateOptions = []
>>>
>>> for style in styles:
for fruit in fruits:
plateOptions.append([style,fruit])
>>> plateOptions
[['chopped', 'apples'], ['chopped', 'bananas'], ['chopped', 'oranges'], ['diced', 'apples'], ['diced', 'bananas'], ['diced', 'oranges'], ['minced', 'apples'], ['minced', 'bananas'], ['minced', 'oranges']]

The above example can be condensed down to:

>>> styles = ['chopped','diced','minced']
>>> fruits = ['apples','bananas','oranges']
>>> plateOptions = [[style,fruit] for style in styles for fruit in fruits]
>>> plateOptions
[['chopped', 'apples'], ['chopped', 'bananas'], ['chopped', 'oranges'], ['diced', 'apples'], ['diced', 'bananas'], ['diced', 'oranges'], ['minced', 'apples'], ['minced', 'bananas'], ['minced', 'oranges']]

Tip #3 — Be creative but don’t get carried away

When using list comprehension it’s easy to get carried away. Below is an example of myself getting carried away. I was attempting to assign an element in a list the value ‘DragronFruit’.

>>> fruits = ['apples','bananas','oranges']
>>> [fruits.__setitem__(ix, 'DragonFruit') for ix, fruit in enumerate(fruits) if 'apples' == fruit]
>>> fruits
['DragonFruit', 'bananas', 'oranges']

As you can see, I assigned the value ‘DragonFruit’ to the [0] element in the fruits list. This example may look like a workaround; however, it’s poor coding practice. Not only does it go against the purpose of list comprehension but it also makes my code more difficult to read. List comprehension is intended for generating a list, not assigning values in other lists.

Conclusion

List comprehension gives us quite a few options when transforming one list to another list. It’s no question that it makes our code more brief. It’s important to takeaway the flexibility of list comprehension while remembering to make your code as readable as possible.

Thanks For Reading. If you enjoyed this post please give it 50 claps (Yes, 50 claps 😊) 👏👏👏👏

--

--