python - How to concatenate nested lists together into a new nested list using a loop? -


i can't seem figure out how this... have example of want outcome be, can't figure out how loop.

celts = [["bass", 1,2,3],          ["bradley", 7,8,9]]  celts2 = [["bass", 4,5,6],           ["bradley", 1,2,3]]  celts3 = [["bass", 8, 5, 2],           ["bradely", 7,4,1]]  new = celts[0] + celts2[0] + celts3[0], celts[1] + celts2[1] + celts3[1],  print new 

result is:

(['bass', 1, 2, 3, 'bass', 4, 5, 6, 'bass', 8, 5, 2], ['bradley', 7, 8, 9, 'bradley', 1, 2, 3, 'bradely', 7, 4, 1]) 

using list comprehension (which is type of loop):

celts = [["bass", 1,2,3],          ["bradley", 7,8,9]] celts2 = [["bass", 4,5,6],           ["bradley", 1,2,3]]    celts3 = [["bass", 8, 5, 2],           ["bradely", 7,4,1]]  new = [celts[i] + celts2[i] + celts3[i] in range(len(celts))]  >>> print new [['bass', 1, 2, 3, 'bass', 4, 5, 6, 'bass', 8, 5, 2], ['bradley', 7, 8, 9, 'bradley', 1, 2, 3, 'bradely', 7, 4, 1]] 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -