Tim Pitzker has already answered the case when you use the same attribute for each iterable. If you use different attributes of the same type, you can do it as follows (using complex numbers as a ready-made class that has two attributes of the same type):
In Python 2:
>>> a = [1+4j, 7+0j, 3+6j, 9+2j, 5+8j] >>> b = [2+5j, 8+1j, 4+7j, 0+3j, 6+9j] >>> keyed_a = ((n.real, n) for n in a) >>> keyed_b = ((n.imag, n) for n in b) >>> from itertools import chain >>> sorted_ab = zip(*sorted(chain(keyed_a, keyed_b), key=lambda t: t[0]))[1] >>> sorted_ab ((1+4j), (8+1j), (3+6j), 3j, (5+8j), (2+5j), (7+0j), (4+7j), (9+2j), (6+9j))
Since iterator returns in zip() in Python 3, we need to force it to a list before trying to index it:
>>>
source share