Fix Python – Sorting a Python list by two fields [duplicate]

Question

Asked By – half full

I have the following list created from a sorted csv

list1 = sorted(csv1, key=operator.itemgetter(1))

I would actually like to sort the list by two criteria: first by the value in field 1 and then by the value in field 2. How do I do this?

Now we will see solution for issue: Sorting a Python list by two fields [duplicate]


Answer

like this:

import operator
list1 = sorted(csv1, key=operator.itemgetter(1, 2))

This question is answered By – mouad

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0