Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Lists of elements to List of Tuples

Tags:

python

How can I combine a List of elements with a List of Tuples (as shown below) ?

a = ['x', 'y', 1234]
b = [('Broad Street', 'NY'), ('Park Street', 'CA')]

Expected output:

[('x', 'y', 1234, 'Broad Street', 'NY'), ('x', 'y', 1234, 'Park Street',  'CA')]
like image 804
marie20 Avatar asked Dec 14 '25 22:12

marie20


1 Answers

Use extended iterable unpacking to build the tuples of the expected result:

res = [(*a, *bi) for bi in b]
print(res)

Output

[('x', 'y', 1234, 'Broad Street', 'NY'), ('x', 'y', 1234, 'Park Street', 'CA')]

As an alternative, use:

tuple_a = tuple(a)
res = [tuple_a + bi for bi in b]
like image 167
Dani Mesejo Avatar answered Dec 16 '25 16:12

Dani Mesejo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!