Sort a list of tuples by 2nd item (integer value)
Try using the key
keyword with sorted()
.
sorted([('abc', 121),('abc', 231),('abc', 148), ('abc',221)],
key=lambda x: x[1])
key
should be a function that identifies how to retrieve the comparable element from your data structure. In your case, it is the second element of the tuple, so we access [1]
.
For optimization, see jamylak's response using itemgetter(1)
, which is essentially a faster version of lambda x: x[1]
.
Sorting a list of tuples by 2nd item not working in python
sorted
returns a sorted version of the list you hand over.
The list itself is unmodified. You're always printing the unsorted list. Try:
sorted_list_1 = sorted(l, key=lambda x: x[0])
sorted_list_2 = sorted(l, key=lambda x: x[2])
print (sorted_list1, sorted_list2)
How to sort a list/tuple of lists/tuples by the element at a given index?
sorted_by_second = sorted(data, key=lambda tup: tup[1])
or:
data.sort(key=lambda tup: tup[1]) # sorts in place
The default sort mode is ascending. To sort in descending order use the option reverse=True
:
sorted_by_second = sorted(data, key=lambda tup: tup[1], reverse=True)
or:
data.sort(key=lambda tup: tup[1], reverse=True) # sorts in place
Sort by elements of a list of tuples
Hoo boy, this was a big XY problem. You want to sort by the third character of the second element of each tuple. Use the sorted
built-in with its kwarg key
.
lst = [('abc', 'bcd'), ('abc', 'zza')]
result = sorted(lst, key=lambda t: t[1][2])
key
here accepts a function that is given each element in turn, and does something to it that is compared. In this case it returns the second element's third element (t[1][2]
). Each element of the outer list is a tuple. Each tuple's second element exists in index 1
, each second element's third character exists in index 2
.
Sorting a list of tuples by the first value
The problem is that you have strings and so it's sorting them alphabetically. Convert to integers:
foundPlayers.append((int(row['Average PTS']), int(row['PlayerCode'])))
python sort list of tuples in list by value
This should do it:
l = [[('Armin', 1.0), ('Doris', 0.2240092377397959)], [('Benjamin', 1.0), ('Doris', 0.3090169943749474)], [('Caroline', 1.0), ('Benjamin', 0.2612038749637414)], [('Doris', 1.0), ('Benjamin', 0.3090169943749474)], [('Ernst', 1.0), ('Benjamin', 0.28989794855663564)]]
sorted(l, key=lambda x: x[1][1], reverse=True)
Explanation:
Passing the key parameter in sorted allows you to sort an object by a key that you specify. In this case, the key we wan to sort on is the second value in the second item of the list. Passing the reverse=True
parameter sorts the list in descending order.
How to sort a list of tuples which elements are of mixed nature?
Use sorted with a key lambda:
sorted_list = sorted(mylist, key=lambda x: x[1])
Related Topics
Sort a List by Multiple Attributes
Annotate Bars With Values on Pandas Bar Plots
How to Select a Variable by (String) Name
Use a List of Values to Select Rows from a Pandas Dataframe
Parse Date String and Change Format
How to Install a Python Package With a .Whl File
Why Is Python Running My Module When I Import It, and How to Stop It
Delete a Column from a Pandas Dataframe
Permanently Add a Directory to Pythonpath
Speed Up Millions of Regex Replacements in Python 3
How to Use the Apply() Function For a Single Column
Groupby Value Counts on the Dataframe Pandas
Updating Gui Elements in Multithreaded Pyqt
How to Read a Text File into a String Variable and Strip Newlines