Yusuf Ning Yusuf Ning - 4 months ago 16
Python Question

concatenating 2 list in python

this is what I have. I want to concatenate this 2 list together

first_name = ['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie',
'Carl', 'Ned', 'Barney', 'Lenny', 'Otto', 'Seymour']

last_name = ['Simpson', 'Simpson', 'Simpson', 'Simpson', 'Simpson',
'Carlson', 'Flanders', 'Gumble', 'Leonard', 'Mann', 'Skinner']

for (i, j) in zip(first_name, last_name):
print (first_name[i] + " " + last_name[j])


But there is an error message saying

TypeError: list indices must be integers, not str


on the line with the "print" statement

Answer

your loop actually iterates through the elements, not the indices

for (f, l) in zip(first_name, last_name):
    print (f + " " + l)