Home > OS >  Concatenate list element of three different based on index into one list
Concatenate list element of three different based on index into one list

Time:10-19

I have the following three lists of which I'd like to combine each list element by index creating a new list.

Before:

list_numbers = [55900, 44560, 49510, 49509, 49519, 49556, 49586]

list_names = ['Richard White', 'Susan Pierce', 'Kim Note', 'John Lee', 'Jennifer Six', 'Maria Cruz', 'Martin Lewis']

list_grades = ['100', '46', '76', '74', '50', '67', '79']

What I'd like to receive as a result:

list_final = ['55900 Richard White 100', '44560 Susan Pierce 46', '49510 Kim Note 76', '49509 John Lee 74', '49519 Jennifer Six 50', '49556 Maria Cruz 67', '49586 Martin Lewis 79']

Once the list is created it is supposed to be sorted by the first 4 numbers in the character list elements.

Thanks, everyone!

CodePudding user response:

new= [f"{a} {b} {c}" for a,b,c in zip(list_numbers,list_names, list_grades )]

CodePudding user response:

You can try to run loop and concatenate all values as single string as below:

[f'{list_numbers[l]} {list_names[l]} {list_grades[l]}' for l in range(len(list_grades))]

Or

[' '.join(map(str, z)) for z in zip(list_numbers,list_names, list_grades )]

Output:

['55900 Richard White 100',
'44560 Susan Pierce 46',
'49510 Kim Note 76',
'49509 John Lee 74',
'49519 Jennifer Six 50',
'49556 Maria Cruz 67',
'49586 Martin Lewis 79']
  • Related