Nested list comprehension with two lists

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

    The above answers will suffice for your question but I wanted to provide you with a list comprehension solution for reference (seeing as that was your initial code and what you're trying to understand).

    Assuming the length of both lists are the same, you could do:

    [l1[i] + l2[i] for i in range(0, len(l1))]
    

提交回复
热议问题