Sorting a zipped object in python 3

python, sorting

Solution

`zip()` in Python 3 returns an iterator; the inputs are zipped as you request elements from it. Iterators are not sortable, no.

You can use the `sorted()` function to 'draw out' the elements and return a sorted list from them:

sorted(zip(a, b))

You could also convert the `zip()` object to a list by calling `list()` on it, then sort that result in-place with the `list.sort()` method, but that's then more work than just using the `sorted()` function.

`sorted()` takes the same keyword arguments as `list.sort()` does, so you can still use the same `key` function:

Demo:

>>> a = [3, 9, 2, 24, 1, 6]
>>> b = ['a', 'b', 'c', 'd', 'e']
>>> sorted(zip(a, b))
[(1, 'e'), (2, 'c'), (3, 'a'), (9, 'b'), (24, 'd')]
>>> sorted(zip(a, b), key=lambda x: x[1])
[(3, 'a'), (9, 'b'), (2, 'c'), (24, 'd'), (1, 'e')]

Also see What is the difference between `sorted(list)` vs `list.sort()` ? python

Problem

I've a `zip` object and I want to sort it(based on a specific key). I've already seen How do I sort a zipped list in Python? but the accepted answer does not work in python 3.6 anymore. For example ``` In [6]: a = [3,9,2,24,1,6] In [7]: b = ['a','b','c','d','e'] In [8]: c = zip(a,b) In [9]: c Out[9]: <zip at 0x108f59ac8> In [11]: type(c) Out[11]: zip In [12]: c.sort() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-12-a21727fa8976> in <module>() ----> 1 c.sort() AttributeError: 'zip' object has no attribute 'sort' # Wanted this to be sorted by the first element In [13]: for l,r in c: print(l,r) 3 a 9 b 2 c 24 d 1 e ``` In other words how do I make the zip iteration order conform to the sorting order. I'm aware that converting a zip to a list of tuples will allow me to fix this, but I want to retain the zipped object (as it used to be in the good old days of python2.7)

Original source

Related problems