-5

I'm having trouble figuring out how to merge two lists so that each item in each list is merged with the other one based on what place its in. Both of the lists are of equal length. For example:

xList=[abc,zxc,qwe]

yList=[1,2,3]

I need

[abc1,zxc2,qwe3]. 

I'm hoping I can make a loop that will be able to handle really long lists that will do this for me.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
volrath
  • 11
  • 1

2 Answers2

2

zip is your friend:

>>> xList=['abc','zxc','qwe']
>>> yList=[1,2,3]
>>> [x+str(y) for x,y in zip(xList,yList)]
['abc1', 'zxc2', 'qwe3']
Julien
  • 13,986
  • 5
  • 29
  • 53
dawg
  • 98,345
  • 23
  • 131
  • 206
0

Using map + lambda:

xList=['abc','zxc','qwe']
yList=[1,2,3]
print(list(map(lambda x,y: x+str(y),xList,yList)))

Output:

['abc1', 'zxc2', 'qwe3']
U13-Forward
  • 69,221
  • 14
  • 89
  • 114