Home > Net >  Joining two lists (alternating)
Joining two lists (alternating)

Time:11-10

I have two lists that I am joining but running into issues.

First list is called header

header= list(last)
header

['AMC', 'AMD', 'EDU', 'F', 'FCEL', 'LCID']

Second list is called lastlist= lastr.values.tolist()

lastlist

[[22.418, 1.627, 0.121, 2.365, 1.019, 4.574]]

To add the two lists to gether, we use the zip function.

master = []

for sym, num in zip(header, lastlist): master.append(' '.join((sym, num)))

print(master)

However, unfortunately I get this error:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-195-33ee9da2ea12> in <module> 2 3 for sym, num in zip(header, lastlist): ----> 4 master.append(' '.join((sym, num))) 5 6 print(master)

TypeError: sequence item 1: expected str instance, list found

I get the error above. I want to my new list to be in this format

master = [AMC 22.418, AMD 1.627,...]

Any help would be appreciated.

Thanks

Yasser

CodePudding user response:

Try this one instead, you miss some quote ( and str(num):

If there is any questions, please ask.

for sym, num in zip(header, lastlist[0]):
    master.append((' '.join((sym, str(num)))))
    
print(master)
    

Output:

['AMC 22.418', 'AMD 1.627', 'EDU 0.121', 'F 2.365', 'FCEL 1.019', 'LCID 4.574']
  •  Tags:  
  • list
  • Related