How to Sort a Tuple in Python?

As a data scientist working with large datasets of US cities and states, I got a requirement to sort tuples containing this information. Sorting tuples allows for easier data analysis and visualization. Python provides various methods to do so. In this tutorial, I will explain how to sort a tuple in Python with various examples.

What is a Tuple in Python?

A tuple is an ordered, immutable collection of elements in Python. Unlike lists, tuples cannot be modified once created. Tuples are defined using parentheses () and elements are separated by commas.

us_city = ("New York", "New York", 8336817)

Now, let me show you how to sort a tuple in Python.

Check out How to Split a Tuple in Python?

Sort a Tuple using the sorted() Function

To sort a tuple in Python, we can use the built-in sorted() function in Python. This function returns a new sorted list from the elements of the input tuple.

us_cities = ("Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia")
sorted_cities = tuple(sorted(us_cities))
print(sorted_cities)
# Output: ('Chicago', 'Houston', 'Los Angeles', 'Philadelphia', 'Phoenix')

I executed the above Python code using VS code, and you can see the exact output in the screenshot below:

Sort a Tuple in Python

Check out How to Reverse a Tuple in Python?

Sort a Tuple by a Specific Element

When working with tuples that contain multiple elements, we may want to sort the tuple based on a specific element. To do this, we can use the key parameter in the sorted() function.

us_cities_data = [
    ("New York", "New York", 8336817),
    ("Los Angeles", "California", 3898747),
    ("Chicago", "Illinois", 2746388),
    ("Houston", "Texas", 2320268),
    ("Phoenix", "Arizona", 1680992)
]

sorted_cities_by_population = sorted(us_cities_data, key=lambda x: x[2])
print(sorted_cities_by_population)
# Output: [('Phoenix', 'Arizona', 1680992), ('Houston', 'Texas', 2320268), ('Chicago', 'Illinois', 2746388), ('Los Angeles', 'California', 3898747), ('New York', 'New York', 8336817)]

In this example, we use a lambda function as the key to sort the tuples based on the third element (population).

Here is the exact output in the screenshot below:

Python Sort a Tuple

Read How to Pass a Tuple as an Argument to a Function in Python?

Sort a List of Tuples

When dealing with a list of tuples, we can sort the list using the sort() method or the sorted() function.

us_cities_data = [
    ("New York", "New York", 8336817),
    ("Los Angeles", "California", 3898747),
    ("Chicago", "Illinois", 2746388),
    ("Houston", "Texas", 2320268),
    ("Phoenix", "Arizona", 1680992)
]

us_cities_data.sort(key=lambda x: x[1])  # Sort by state name
print(us_cities_data)
# Output: [('Phoenix', 'Arizona', 1680992), ('Los Angeles', 'California', 3898747), ('Chicago', 'Illinois', 2746388), ('New York', 'New York', 8336817), ('Houston', 'Texas', 2320268)]

Read How to Sort by the Second Element in a Tuple in Python?

Sort a Tuple in Python Example

Now, let me show you a real example.

Let’s consider we have a dataset of US cities, states, and their populations. Our goal is to sort this data based on different criteria.

us_cities_data = [
    ("New York", "New York", 8336817),
    ("Los Angeles", "California", 3898747),
    ("Chicago", "Illinois", 2746388),
    ("Houston", "Texas", 2320268),
    ("Phoenix", "Arizona", 1680992),
    ("Philadelphia", "Pennsylvania", 1584064),
    ("San Antonio", "Texas", 1532233),
    ("San Diego", "California", 1423851),
    ("Dallas", "Texas", 1343573),
    ("San Jose", "California", 1021795)
]

# Sort by city name
sorted_by_city = sorted(us_cities_data, key=lambda x: x[0])
print("Sorted by city name:")
print(sorted_by_city)

# Sort by state name
sorted_by_state = sorted(us_cities_data, key=lambda x: x[1])
print("\nSorted by state name:")
print(sorted_by_state)

# Sort by population in descending order
sorted_by_population_desc = sorted(us_cities_data, key=lambda x: x[2], reverse=True)
print("\nSorted by population (descending):")
print(sorted_by_population_desc)

Output:

Sorted by city name:
[('Chicago', 'Illinois', 2746388), ('Dallas', 'Texas', 1343573), ('Houston', 'Texas', 2320268), ('Los Angeles', 'California', 3898747), ('New York', 'New York', 8336817), ('Philadelphia', 'Pennsylvania', 1584064), ('Phoenix', 'Arizona', 1680992), ('San Antonio', 'Texas', 1532233), ('San Diego', 'California', 1423851), ('San Jose', 'California', 1021795)]

Sorted by state name:
[('Phoenix', 'Arizona', 1680992), ('Los Angeles', 'California', 3898747), ('San Diego', 'California', 1423851), ('San Jose', 'California', 1021795), ('Chicago', 'Illinois', 2746388), ('New York', 'New York', 8336817), ('Philadelphia', 'Pennsylvania', 1584064), ('Dallas', 'Texas', 1343573), ('Houston', 'Texas', 2320268), ('San Antonio', 'Texas', 1532233)]

Sorted by population (descending):
[('New York', 'New York', 8336817), ('Los Angeles', 'California', 3898747), ('Chicago', 'Illinois', 2746388), ('Houston', 'Texas', 2320268), ('Phoenix', 'Arizona', 1680992), ('Philadelphia', 'Pennsylvania', 1584064), ('San Antonio', 'Texas', 1532233), ('San Diego', 'California', 1423851), ('Dallas', 'Texas', 1343573), ('San Jose', 'California', 1021795)]

In this example, we demonstrate sorting the US city and state data based on city name, state name, and population in descending order.

Conclusion

In this tutorial, I explained how to sort a tuple in Python using different methods, especially the sorted() function. I have also shown a real example.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.