iraciv94
iraciv94

Reputation: 840

Nested structure: List of lists of tuples python

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!

Upvotes: 4

Views: 422

Answers (5)

sahasrara62
sahasrara62

Reputation: 11238

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')]
]

Upvotes: 2

Rakesh
Rakesh

Reputation: 82785

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')]]

Upvotes: 3

HK boy
HK boy

Reputation: 1406

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')]]

Upvotes: 2

Olvin Roght
Olvin Roght

Reputation: 7812

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))]

Upvotes: 1

zipa
zipa

Reputation: 27879

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')]]

Upvotes: 5

Related Questions