DEV Community

Mauricio Panelo
Mauricio Panelo

Posted on

An Infographic About Python List Comprehensions

Python List Comprehensions

I spent way to much time on this infographic.

The good news is I have a template for future programming tips. I predict my time spent on the next graphic will be considerably less.

My inspiration for creating this comes from Simon Høiberg's tips for JavaScript. I recommend checking out his content.

I will say that list comprehensions shouldn't be used for everything. There are cases where it's more readable to write out the for loop (e.g. nested for loops).

List comprehensions are really powerful. You can mimic filter and map expressions using a list comprehension.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# filter numbers greater than 5
x = [n for n in numbers if n > 5]

# map the numbers to a string
x = [str(n) for n in numbers]
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! Let me know what you think of the infographic. I appreciate any feedback.

Top comments (1)

Collapse
 
ronaldgrowe profile image
Ronald Rowe

Keep the tips coming! Great work on the graphic. 👍