4

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!

3
  • 1
    your logic is wrong..for each tuple you are looping through column_to_add. You want to match each index of internal_list to the matching index of column_to_add. Zipa's answer does this through enumerate which provides an index to work with. Rakesh's answer uses zip which 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 Commented May 17, 2019 at 12:39
  • Now it's way clearer, so if I understood correctly using zip or enumerate is completely equivalent. Right? Commented May 17, 2019 at 13:27
  • 1
    They both create iterators (yield on the fly) objects, the objects created are slightly different though, one (zip) is a tuple on the actual matching elements, one is a (index, item) tuple leaving you free to use the index however you like. I prefer enumerate in this case as the code is more explicit. Commented May 17, 2019 at 15:04

5 Answers 5

5

If your data looks like this:

list_of_lists= [[("aaa", ),("bbb", )],[("ccc", ),("ddd", )],[("eee", ),("fff", )]]

You should use:

[[y + (column_to_add[i], ) for y in x] for i, x in enumerate(list_of_lists)]

This produces:

#[[('aaa', 'string1'), ('bbb', 'string1')],
# [('ccc', 'string2'), ('ddd', 'string2')],
# [('eee', 'string3'), ('fff', 'string3')]]
Sign up to request clarification or add additional context in comments.

Comments

3

Using zip and a nested list comprehension

Ex:

list_of_lists= [[("aaa"),("bbb")],[("ccc"),("ddd")],[("eee"),("fff")]]
column_to_add = ["string1", "string2", "string3"]

print([[(i, n) for i in m] for m,n in zip(list_of_lists, column_to_add)])

Output:

[[('aaa', 'string1'), ('bbb', 'string1')],
 [('ccc', 'string2'), ('ddd', 'string2')],
 [('eee', 'string3'), ('fff', 'string3')]]

Comments

2

You will need something likes zip().

First, keep (aaa, bbb) and string1 in pair.

a = [[("aaa"),("bbb")],[("ccc"),("ddd")],[("eee"),("fff")]]
b = ["string1", "string2", "string3"]
zipped_data = list(zip(a, b))

# zipped_data = [(['aaa', 'bbb'], 'string1'), (['ccc', 'ddd'], 'string2'), (['eee', 'fff'], 'string3')]

Then, let string1 make pair with each iterator of the tuple (aaa, bbb).

new_list = []
for u in zipped_data:
    new_list.append([(u[0][0], u[1]), (u[0][1], u[1])])
print(new_list)

The output is

[[('aaa', 'string1'), ('bbb', 'string1')], [('ccc', 'string2'), ('ddd', 'string2')], [('eee', 'string3'), ('fff', 'string3')]]

Comments

2
list_of_lists= [[("aaa"),("bbb")],[("ccc"),("ddd")],[("eee"),("fff")]]
column_to_add = ["string1", "string2", "string3"]
res  = list(map(lambda x,y: [(i,y) for i in x], list_of_lists, column_to_add))
print(res)

output

[
   [('aaa', 'string1'), ('bbb', 'string1')], 
   [('ccc', 'string2'), ('ddd', 'string2')], 
   [('eee', 'string3'), ('fff', 'string3')]
]

Comments

1

You can use list comprehensions.

lst = [[("aaa",), ("bbb",)], [("ccc",), ("ddd",)], [("eee",), ("fff",)]]
col = ["string1", "string2", "string3"]

result = [[(*tup, col[i]) for tup in lst[i]] for i in range(len(lst))]

Output:

[[('aaa', 'string1'), ('bbb', 'string1')], [('ccc', 'string2'), ('ddd', 'string2')], [('eee', 'string3'), ('fff', 'string3')]]

Upd.

It could be more "safe" to use length of col as limit of range.

result = [[(*tup, col[i]) for tup in lst[i]] for i in range(len(col))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.