Converting List Comprehensions to For Loops in Python

It’s just a shorter way of expressing a list.

li = [row[index] for row in outer_list]

is equivalent to:

li = []
for row in outer_list:
    li.append(row[index])

Once you get used to the syntax, it becomes a tidy way of creating lists (and other iterables).

Leave a Comment