Nested list comprehension with two lists

后端 未结 5 908
误落风尘
误落风尘 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:03

    this sequence

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

    is equivalent to

    res =[]
    for x in l2:
        for y in l1:
            res.append(x+y)
    

提交回复
热议问题