Ad

How To Merge Lists In Python?Merging Python Lists (0,1,2) & (A,B,C) To Get (0A, 1B, 2C)

- 1 answer

I have two lists that I need to merge, but the normal merging questions that I saw didn't seem to help.

l1 = (0,1,2,3)
l2 = ('A','B','C','D')

And I need it to become

((0,'A'), (2,'B'), (3,'C'))
Ad

Answer

>>> A = ('0','1','2','3')
>>> B = ('A','B','C','D')
>>> [x + y for x, y in zip(A, B)]
['0A', '1B', '2C', '3D']

For any number of lists

>>> lists = (A, B)
>>> [''.join(x) for x in zip(*lists)]
['0A', '1B', '2C', '3D']
Ad
source: stackoverflow.com
Ad