Converting List Comprehensions to For Loops in Python

前端 未结 4 1806
轻奢々
轻奢々 2020-12-02 00:51

I understand the importance of list comprehensions, but do not understand their inner-workings, thus am not able to understand them in simpler terms such as I would a for lo

4条回答
  •  长情又很酷
    2020-12-02 01:38

    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).

提交回复
热议问题