pmv - 25 days ago 5x

Python Question

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 ?

Thanks

PMV

Answer

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']
```

Source (Stackoverflow)

Comments