pmv pmv - 1 year ago 70
Python Question

python manipulations on lists

I have 2 lists

l1 = ('A','B','C')
l2 = ('X','Y','Z')

I want to create a list based on these 2

result = ('A is same as X', 'B is same as Y', 'C is same as Z')

when I concatenate , I do not get the result I expect
How can I combine the lists ?

Answer Source

You can zip the lists together which will combine the two lists into a list of tuples and then iterate over those tuples.

z = zip(l1,l2) #[(A,X), (B,Y), (C,Z)]

result = ["{0} is the same as {1}".format(t[0], t[1]) for t in z]

# ['A is the same as X', 'B is the same as Y', 'C is the same as Z']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download