python: combine each element of a list to a list of tuple

Viewed 605

I have a list of numbers:

list_A = [1,2,3,4,5]

I have another list of tuples:

list_B = [(10, 11), (20,21), (30,31), (40,41), (50,51)]

I want to combine list_A and list_B, become like below:

[(10,11,1), (20,21,2), (30,31,3), (40,41,4), (50,51,5)]

what's the most efficient way to do such?

7 Answers

Double zip:

[*zip(*zip(*list_B), list_A)]

Comparison of the solutions so far:

1035 ns  1047 ns  1049 ns  [(*b, a) for a, b in zip(list_A, list_B)]
1138 ns  1140 ns  1141 ns  [(list_B[index][0], list_B[index][1], list_A[index]) for index in range(len(list_A))]
 963 ns   967 ns   993 ns  [list_B[i] + (list_A[i],) for i in range(len(list_A))]
 805 ns   820 ns   826 ns  [i + (j,) for j,i in zip(list_A,list_B)]
 947 ns   952 ns   965 ns  [*zip(*zip(*list_B), list_A)]

Benchmark code (Try it online!):

from timeit import repeat

list_A = [1, 2, 3, 4, 5]
list_B = [(10, 11), (20, 21), (30, 31), (40, 41), (50, 51)]

E = [
    '[(*b, a) for a, b in zip(list_A, list_B)]',
    '[(list_B[index][0], list_B[index][1], list_A[index]) for index in range(len(list_A))]',
    '[list_B[i] + (list_A[i],) for i in range(len(list_A))]',
    '[i + (j,) for j,i in zip(list_A,list_B)]',
    '[*zip(*zip(*list_B), list_A)]',
]

for _ in range(3):
    for e in E:
        number = 100000
        times = sorted(repeat(e, globals=globals(), number=number, repeat=3))
        print(*('%4d ns ' % (t / number * 1e9) for t in times), e)
    print()

Use a list comprehension to unpack and repack the elements, and to get the lists together for the iterations you can use zip.

list_A = [1, 2, 3, 4, 5]
list_B = [(10, 11), (20, 21), (30, 31), (40, 41), (50, 51)]

output = [(*b, a) for a, b in zip(list_A, list_B)]

>>> [(10, 11, 1), (20, 21, 2), (30, 31, 3), (40, 41, 4), (50, 51, 5)]

Using zip

res = [i + (j,) for j,i in zip(list_A,list_B)]

output:

[(10, 11, 1), (20, 21, 2), (30, 31, 3), (40, 41, 4), (50, 51, 5)]

If the tuples in list_B are always of 2 items it would be both clearer and faster to unpack it as such:

[(a, b, c) for (a, b), c in zip(list_B, list_A)]

Borrowing @don'ttalkjustcode's benchmark code:

1091 ns  1109 ns  1109 ns  [(*b, a) for a, b in zip(list_A, list_B)]
1183 ns  1189 ns  1205 ns  [(list_B[index][0], list_B[index][1], list_A[index]) for index in range(len(list_A))]
1035 ns  1052 ns  1053 ns  [list_B[i] + (list_A[i],) for i in range(len(list_A))]
 843 ns   897 ns   926 ns  [i + (j,) for j,i in zip(list_A,list_B)]
 979 ns   994 ns  1015 ns  [*zip(*zip(*list_B), list_A)]
 760 ns   765 ns   767 ns  [(a, b, c) for (a, b), c in zip(list_B, list_A)]

Try it online!

For each element of B (a tuple), you add each element of A to the tuple. Note that this is only possible when the element of A is a tuple, hence the brackets and the comma.

Using list comprehension:

list_A = [1,2,3,4,5]
list_B = [(10, 11), (20,21), (30,31), (40,41), (50,51)]

[list_B[i] + (list_A[i],) for i in range(len(list_A))]   

Output:

Out[16]: [(10, 11, 1), (20, 21, 2), (30, 31, 3), (40, 41, 4), (50, 51, 5)]
res = [(list_B[index][0], list_B[index][1], list_A[index]) for index in range(len(list_A))])

Output:

[(10, 11, 1), (20, 21, 2), (30, 31, 3), (40, 41, 4), (50, 51, 5)]

You can do it by using for loop

List of numbers:

list_a = [1, 2, 3, 4, 5]

Tuple list of numbers:

list_b = [(10, 11), (20, 21), (30, 31), (40, 41), (50, 51)]

As you know tuples are immutable
So, you can create an empty list:

new_list = []

Let's run a loop to solve:

for i in range(len(list_a)):
        list_together = []
        list_together += list_b[i]
        list_together.append(list_a[i])
        tup = tuple(list_together)
        new_list.append(tup)

Details:

  • Running the loop in the range/length of list_a
  • list_together concatenates both list_a and list_b
  • Append list-a values with list_b in list_together variable when loop runs
  • tup converts the list into tuple and after that append into new_list

Finally, let's print the newly created list as output:

print(new_list)

Output:

[(10, 11, 1), (20, 21, 2), (30, 31, 3), (40, 41, 4), (50, 51, 5)]
Related