I am trying to deal with a nested structure that looks like this:
list_of_lists= [[("aaa"),("bbb")],[("ccc"),("ddd")],[("eee"),("fff")]]
and I need to add a column of elements that looks like this:
column_to_add = ["string1", "string2", "string3"]
The final result should look like this:
[[("aaa", "string1"),("bbb", "string1")],[("ccc", "string2"),("ddd", "string2")],[("eee", "string3"),("fff", "string3")]]
I have tried something like this:
result= []
for internal_list in list_of_lists:
for tuple in internal_list:
for z in tuple:
for new_string in column_to_add:
kk=list(tuple)
result = tuple.append(new_string)
But it does not seem to work at all. Can anyone help me?
Thanks so much in advance!
column_to_add. You want to match each index ofinternal_listto the matching index ofcolumn_to_add. Zipa's answer does this throughenumeratewhich provides an index to work with. Rakesh's answer useszipwhich zips up the two lists where he unpacks and uses them. Both use list comprehension which is similar to a for loop appending items to initial empty list