List of combinations with a list of tuples

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]

>>> print(list(zip(l1, l2)))

[(1, (10, 20)), (2, (30, 40)), (3, (50, 60)), (4, (70, 80))]

      

However, I want it to be just a list of four tuples:

[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

      

I've also tried:

>>> print(list(zip(l1, *l2)))

[(1, 10, 30, 50, 70), (2, 20, 40, 60, 80)]

      

So my question is:

How can I freeze a list with a list of tuples or a list of lists?

+3


source to share


5 answers


A more general way of solving this problem is to consider that both lists must be scaled up / down to the same size as when required.

>>> [(a, ) + b for a, b in zip(l1, l2)]
[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

      

As in your example, the second list has an extra dimension as opposed to the first list, or



  • Convert the first list to a higher size corresponding to the second list
  • Reduce the size of the second list

In this particular case, the first approach was simpler and therefore was the obvious choice

+8


source


>>> l1=[1,2,3,4]
>>> l2=[(10,20),(30,40),(50,60),(70,80)]
>>> [tuple([x] + list(y)) for x,y in zip(l1,l2)]
[(1, 10, 20), (2, 30, 40), (3, 50, 60), (4, 70, 80)]

      



+4


source


You need

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]
print [(a, b[0], b[1]) for a, b in list(zip(l1, l2))]

      

It works like this: zip

creates an array that concatenates the elements l1

andl2

>>> zip (l1, l2)
[(1, (10, 20)), (2, (30, 40)), (3, (50, 60)), (4, (70, 80))]

      

for i, j in zip(l1, l2)

will unpack the values ​​in i and j, sequentially grabbing one element from each array in each iteration of the loop; those. 1

and (10, 20)

in the first iteration, 2

and (30, 40)

in the second, etc.

Then you can shuffle the values ​​to create the tuple you want. You can do it either with (a, b[0], b[1])

or (a, ) + b

. The last approach that Abhijit used and is probably more efficient than mine is based on the fact that you can add tuples (like (1, 2) + (3, 4) + (5,)

equals (1, 2, 3, 4, 5)

).

If what you want to do with the numbers isn't too difficult, you can do it at the beginning of the expression for

instead of creating a tuple. Perhaps you can make your code more compact.

+1


source


If I do this, it does the following:

l1 = [1, 2, 3, 4]
l2 = [(10, 20), (30, 40), (50, 60), (70, 80)]

print [ (a,b[0],b[1]) for a, b in zip(l1, l2)]

      

So, you should know zip()

how to use:

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the sequence of arguments or iterations. The returned list is truncated in length with the length of the shortest sequence of arguments. When there are multiple arguments that are the same length, zip () is like map () with an initial argument of None. With one sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

+1


source


Just by adding another path with an explicit one for the loop, assuming len (l1) == len (l2)

for x in range(0,len(l1)):
    print (l1[x],l2[x][0],l2[x][1])

      

+1


source







All Articles