Aurora_Titanium
Aurora_Titanium

Reputation: 472

Python Parallel Sorting

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

Upvotes: 3

Views: 3021

Answers (1)

Mureinik
Mureinik

Reputation: 312096

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')]

Upvotes: 3

Related Questions