简体   繁体   中英

Appending list of lists

I have two lists of lists, List 1 and List 2 comprising of 12 lists each.

I want to create a new list of lists (List 3) which joins the lists at each corresponding index of List 1 and List 2.

For example the first list in List 3 would be composed of List1[0] + List2[0]....

Please help!

Thanks

Use zip to combine the two lists into a single list of tuples. Then use a list comprehension to combine the tuples however you want (in the question you asked for a list of values combined using the + operator ).

It should be clear how you can change this for any object type/operation:

A = [1,2,3]
B = [4,5,6]
C = [x + y for (x,y) in zip(A,B)]
print(C) # [5,7,9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM