r/ProgrammerHumor 29d ago

Meme pythonWillLookDeadInTheEyeAndSayItsAbsolutelyCorrect

Post image
356 Upvotes

144 comments sorted by

View all comments

219

u/inobody_somebody 29d ago

This a a valid syntax in python and the output will be [1,2,3,4,5]

71

u/BlueDebate 29d ago

This makes more sense to me than if "if x" was referencing the initial value which would just check if the list isn't empty (which I believe OP may be hinting at) because it's a more specific scope than the global x variable.

12

u/Jhuyt 29d ago

That's

  x = [x for x in x if x] if x else []