+ 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)
3 Antworten
+ 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 )
+ 4
+ 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]