You can use the key
parameter to list.sort()
:
my_list.sort(key=lambda x: x[1])
or, slightly faster,
my_list.sort(key=operator.itemgetter(1))
(As with any module, you’ll need to import operator
to be able to use it.)
More Related Contents:
- How to sort a list/tuple of lists/tuples by the element at a given index?
- How to sort a list with two keys but one in reverse order?
- Sorting a tuple that contains tuples
- Sort a list of tuples depending on two elements [duplicate]
- Sorting list based on values from another list
- What’s the difference between lists and tuples?
- How does tuple comparison work in Python?
- Sorting arrays in NumPy by column
- How does swapping of members in tuples (a,b)=(b,a) work internally?
- Ignore python multiple return value
- Quicksort with Python
- When are parentheses required around a tuple?
- Subtracting 2 lists in Python
- How to sort and remove duplicates from Python list? [duplicate]
- Why are python strings and tuples are made immutable?
- Sorting a 2D numpy array by multiple axes
- What is the inverse function of zip in python? [duplicate]
- Sorting a dictionary with lists as values, according to an element from the list
- How does python compare functions?
- Sort list of strings by a part of the string
- Python: How to read huge text file into memory
- How to sort 2d array by row in python?
- Sorting by absolute value without changing to absolute value
- What algorithm does Python’s built-in sort() method use?
- Custom sorting with Pandas
- Python: How to sort a list of dictionaries by several values?
- How to sort a list of x-y coordinates
- Nested lambda statements when sorting lists
- Elegant way to perform tuple arithmetic
- Sorting a pandas series