DEV Community

Discussion on: Ditch These 7 Bad Habits in Python

Collapse
 
jerrynsh profile image
Jerry Ng • Edited

A long and complex list comprehension definitely suffers from being too "smart" and thus ignores readability.

However, when it comes to a short for loop, i.e. generating a list of numbers from a range, no one should be using a regular for loop with append .

# This is way more readable
nums = [i for i in range(10)]

# This is less readable, IMO we shouldn't have to do this
nums = []

for i in range(10):
    nums.append(i)
Enter fullscreen mode Exit fullscreen mode

Throwing in a single conditional if (i.e. the grapes example in the post) is fine IMHO. It really depends on what you and your team agree on at the end of the day. Nothing beats a good consistency in a shared codebase.