+ 2

merge 2 lists?

I want to get after every name the age but i get every time the name and after the nam all the ages. Can someone help me pls? names = ["Art", "Xander", "Arne", "Pieter", "Milan", "Tijl"] ages = [17, 18, 14, 15, 15, 16] names_ages = [] for name in names: names_ages.append(name) for age in ages: names_ages.append(age) print(names_ages)

7th Jul 2021, 8:34 AM
art
art - avatar
3 Answers
+ 6
Use extend() method to add all elements in an iterable (tuple) generated by zip() function names = ["Art", "Xander", "Arne", "Pieter", "Milan", "Tijl"] ages = [17, 18, 14, 15, 15, 16] names_ages = [] for e in zip( names, ages ): names_ages.extend( e ) print( names_ages )
7th Jul 2021, 8:46 AM
Ipang
7th Jul 2021, 8:40 AM
Slick
Slick - avatar
+ 4
With for in range do you can read alterned names and ages names = ["Art", "Xander", "Arne", "Pieter", "Milan", "Tijl"] ages = [17, 18, 14, 15, 15, 16] names_ages = [] for i in range(len(names)): names_ages.append(names[i]) names_ages.append(ages[i]) print(names_ages) Output: ['Art', 17, 'Xander', 18, 'Arne', 14, 'Pieter', 15, 'Milan', 15, 'Tijl', 16]
7th Jul 2021, 12:03 PM
Monica Garcia
Monica Garcia - avatar