0

I have two lists:

A = ['John,Male,20,','Jenny,Female,25','James,Male,30']
B = ['London','Paris','Washington']

Is there a way I can insert the values in the second list into the first list

Hypothetical output:

['John,Male,20,London','Jenny,Female,25,Paris','James,Male,30,Washington']
Nvision
  • 13
  • 3

1 Answers1

0

Use zip to join the lists into lists of pairs. That would give you:

>>> zip(A, B)
[('John,Male,20,', 'London'), ('Jenny,Female,25', 'Paris'), 'James,Male,30', 'Washington')]

Then you can concatenate the strings in each pair using a list comprehension:

>>> [a + b for a, b in zip(A, B)]
['John,Male,20,London', 'Jenny,Female,25,Paris', 'James,Male,30,Washington']
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • perfect , although what would i do if i had to concatenate the ages instead and they were integers not strings ? – Nvision Apr 27 '17 at 19:03