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')]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With