I have list1:
['GM2', 'GM1', 'GM3']
and list2:
['A', 'B', 'C']
How do you sort this in a way where it shows. Basically would like the element at index 0 in list1 to correspond to the element in index 0 in list2.
GM1 B
GM2 A
GM3 C
The builtin zip function will match the corresponding elements so that you get a result of tuples, where each element is made up from an element in list1 and its corresponding element in list2:
>>> list1 = ['GM2', 'GM1' ,'GM3']
>>> list2 = ['A', 'B', 'C']
>>> result = sorted(zip(list1, list2))
>>> result
[('GM1', 'B'), ('GM2', 'A'), ('GM3', 'C')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With