8000 Sorting a list of tuples by the last last elements of the tuple · adrianhust/python_reference@b6f5476 · GitHub
[go: up one dir, main page]

Skip to content

Commit b6f5476

Browse files
committed
Sorting a list of tuples by the last last elements of the tuple
1 parent 841401a commit b6f5476

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

howtos_as_py_files/sort_list_of_tuples_by_ele.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Sebastian Raschka 09/02/2014
22
# Sorting a list of tuples by the last last elements of the tuple
33

4+
5+
# Here, we make use of the "key" parameter of the in-built "sorted()" function
6+
# (al 69E4 so available for the ".sort()" method), which let's us define a function
7+
# that is called on every element that is to be sorted. In this case, our
8+
# "key"-function is a simple lambda function that returns the last item
9+
# from every tuple.
10+
11+
12+
413
a_list = [(1,3,'c'), (2,3,'a'), (1,2,'b')]
514

615
sorted_list = sorted(a_list, key=lambda e: e[::-1])

0 commit comments

Comments
 (0)
0