Nested list comprehension with two lists

后端 未结 5 898
误落风尘
误落风尘 2020-12-08 06:09

I understand how the simple list comprehension works eg.:

[x*2 for x in range(5)] # returns [0,2,4,6,8]

and also I understand how the nested

5条回答
  •  暖寄归人
    2020-12-08 07:06

    The reason it has 9 numbers is because python treats

    [x + y for x in l2 for y in l1 ]
    

    similarly to

    for x in l2:
        for y in l1:
           x + y
    

    ie, it is a nested loop

提交回复
热议问题