Aystealthy
Aystealthy

Reputation: 169

combine 2 lists of list to list of tuples

I'm trying to combine to different nested list into a list of tuples (x,y) where x comes from the first nested list and y from the second nested list.

nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

when you combine them it should be:

result = [(1,.0833), (2,.0833), (3,.0833), (3,.2), (0,.175), (3,.175), (1,.2)]

my approach is that i need to iterate through the list of lists and join them 1 at a time. I know to iterate through 1 nested list like so:

for list in nested_list1:
    for number in list:
        print(number)

but I can't iterate through 2 nested list at the same time.

for list, list in zip(nested_list1, nested_list2):
    for number, prob in zip(list,list):
        print(tuple(number, prob)) #will not work

any ideas?

Upvotes: 1

Views: 214

Answers (8)

mad_
mad_

Reputation: 8273

Use two times zip and flatten the list

from functools import reduce
reduce(lambda x,y: x+y,[(zip(i,j)) for i,j in zip(nested_list1,nested_list2)])

You can flatten using chain as well

from itertools import chain
list(chain(*[(zip(i,j)) for i,j in zip(nested_list1,nested_list2)]))

output

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

Upvotes: 0

Austin
Austin

Reputation: 26039

You could do a double zip through lists:

lst1 = [[1, 2, 3],[3],[0, 3],[1]]
lst2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

print([(u, v) for x, y in zip(lst1, lst2) for u, v in zip(x, y)])

Or use itertools.chain.from_iterable to flatten list and zip:

from itertools import chain

lst1 = [[1, 2, 3],[3],[0, 3],[1]]
lst2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]

print(list(zip(chain.from_iterable(lst1), chain.from_iterable(lst2))))

Upvotes: 1

Dawid Fieluba
Dawid Fieluba

Reputation: 1301

result = []
[result.extend(list(zip(x, y))) for x in nested_list1 for y in nested_list2]
print(result)

Upvotes: 0

Jay
Jay

Reputation: 24895

One way is to convert both the nested lists into full lists and then use zip. Sample code below:

>>> nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
>>> nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]
>>> new_list1 = [x for val in nested_list1 for x in val]
>>> new_list2 = [x for val in nested_list2 for x in val]
>>> print new_list1
[1, 2, 3, 3, 0, 3, 1]
>>> print new_list2
[0.0833, 0.0833, 0.0833, 0.2, 0.175, 0.175, 0.2]
>>> new_val = zip(new_list1, new_list2)
>>> print new_val
[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

Upvotes: 0

Rocky Li
Rocky Li

Reputation: 5958

This one liner will achieve what you want.

reduce(lambda x, y: x+y, [[(i, j) for i, j in zip(x,y)] for x, y in zip(nested_list1, nested_list2)])

Upvotes: 0

jpp
jpp

Reputation: 164653

There are 2 errors in your code:

  1. You shadow built-in list twice and in a way that you can't differentiate between two variables. Don't do this.
  2. You use tuple(x, y) to create a tuple form 2 variables. This is incorrect, as tuple takes one argument only. To construct a tuple of two variables just use syntax (x, y).

So this will work:

for L1, L2 in zip(nested_list1, nested_list2):
    for number, prob in zip(L1, L2):
        print((number, prob))

More idiomatic would be to flatten your nested lists; for example, via itertools.chain:

from itertools import chain

res = list(zip(chain.from_iterable(nested_list1),
               chain.from_iterable(nested_list2)))

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

Upvotes: 0

Netwave
Netwave

Reputation: 42678

Use itertools.chain:

>>> nested_list1 = [[1, 2, 3],[3],[0, 3],[1]]
>>> nested_list2 = [[.0833, .0833, .0833], [.2], [.175, .175], [.2]]
>>> import itertools
>>> res = list(zip(itertools.chain.from_iterable(nested_list1), itertools.chain.from_iterable(nested_list2)))
>>> res
[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

Upvotes: 1

rahlf23
rahlf23

Reputation: 9019

Flatten your lists and then pass to zip():

list1 = [item for sublist in nested_list1 for item in sublist]
list2 = [item for sublist in nested_list2 for item in sublist]
final = list(zip(list1, list2))

Yields:

[(1, 0.0833), (2, 0.0833), (3, 0.0833), (3, 0.2), (0, 0.175), (3, 0.175), (1, 0.2)]

Upvotes: 0

Related Questions