Python sort list of tuples using the sort strategy necessary vital which sorts lists in location. And additionally supplies a convenient means to kind tuples based on the index of tuple facets.

You deserve to sort the list of tuples by first and also second facets by utilizing indexes or values.

You watching: Python sort list by first element

tup<0>to sort on initially tuple elementtup<1>to type on second and also so on.

1. Example sort by the First facet value

my_data = <("P", 1), ("M", 3), ("A", 2), ("B", 4)>my_data.sort(key=lambda tup:tup<1>)print(my_data)Output:

<(‘P’, 1), (‘A’, 2), (‘M’, 3), (‘B’, 4)>

2. Example type by the 2nd aspect value

my_information = <("P", 1), ("M", 3), ("A", 2), ("B", 4)>my_data.sort(key=lambda tup:tup<0>)print(my_data)Output:

<(‘A’, 2), (‘B’, 4), (‘M’, 3), (‘P’, 1)>

How to sort a list of tuples by the initially facet alphabetically in python?

You have actually a list of tuples like this:

<("peter",1), ("mary",5), ("anthony",6), ("brandon",4)>And wanted to sort this list and obtain somepoint like:

<("anthony", 6), ("brandon", 4), ("mary", 5), ("peter", 1)>Answer: You have the right to perform it utilizing a sort() or sorted() functions.

sorted() function

my_data = <("peter", 1), ("mary", 5), ("anthony", 6), ("brandon", 4)>sorted_data = sorted(my_data)print(sorted_data)or sort() feature

my_information = <("peter", 1), ("mary", 5), ("anthony", 6), ("brandon", 4)>my_data.sort()print(my_data)

Python sort list of tuplesdescending

Here is an example of sorting the list of tuples initially by descending order and alphabetically in python 3.

lst is a list of tuples via each tuple holding the worth in the format (worth, key). And you desire to sort it based on the key which is at the index 0 of the tuple. In that situation, you should be calling the .sort() with secrets as:

from operator import itemgetterlst = <("C", 1), ("A", 5), ("B", 6), ("D", 4)>lst.sort(key=itemgetter(0))print(lst)Output: <(‘A’, 5), (‘B’, 6), (‘C’, 1), (‘D’, 4)>

Python sort list of tuples by multiple elements

lst = <(12, "tall", "blue", 1), (2, "short", "red", 9), (4, "tall", "blue", 13)>s = sorted(lst, key=lambda x: (x<2>, x<3>))print(s)Output:


How to type the list of tuples by the last facet in python?

Answer: Specify thekeydispute in thesortedfeature.

Thesortedattribute (and thelist.sortmethod) has actually an optionalkeythe dispute which specifies what to type the list on.

tuple1 = <(1, 3), (3, 2), (2, 1)>ntuple = sorted(tuple1, key=lambda x: x<-1>)print(ntuple)Output: <(2, 1), (3, 2), (1, 3)>

Do comment if you have any doubts around this

See more: The 8 Best Latex Foam Contour Pillow Of 2021, Organic Latex Contour Pillow

Note:IDE: PyCharm 2020.1.1 (Community Edition)macOS 10.15.4Python 3.7All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.



Degree in Computer Science and Engineer: App Developer and also has actually multiple Programming languages suffer. Enthusiasm for technology & favor learning technical.

Share this:


Python Python Function, Python Questions, Python Tuple

Post navigation

Difference in between sort vs sorted Python Function
Python kind string | alphabetically letters ascending/descending order

Leave a ReplyCancel reply

This site supplies Akismet to alleviate spam. Discover exactly how your comment data is processed.

Search TopicSearch for:
Python Start
Python Basic
Python Data varieties, Variables & DS
Python Condition Statements & Loops
Python Documents Handling
Typecasting | Type Convariation in Python
Python Date and also Time functions
Python exception Handling & Error Handling
Recent PostsCategories
Subscribe to

Enter email resolve to subscribe and also get new write-ups by email.

Join 17 other subscribers

Email Address


Loading Comments...
Write a Comment...
Email (Required)Name (Required)Website
Send to Email AddressYour NameYour Email Address
Message was not sent - inspect your email addresses!
Email inspect failed, please try again
Sorry, your blog cannot share posts by email.