# Sort Tuples in Increasing Order by any key in Python program

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to sort a list of tuples in increasing order by nth index key. For example, we have a list of tuples [(2, 2), (1, 2), (3, 1)] then, we have to sort it using 0th index element. The output for that list would be [(1, 2), (2, 2), (3, 1)].

We can achieve this by using the sorted method. We have to pass a key while giving the list to the sorted function. Here, the key is the index on which the sorting is based.

sorted takes a list and returns that list in ascending order of increasing order. If you want to get the list in descending order then, set the reverse keyword argument to True in the sorted function.

Let's see the steps to solve our problem.

## Algorithm

1. Initialize list of tuples and key
2. Define a function. 2.1. Return key-th index number.
3. Pass list of tuples and function to the sorted function. We have to pass function name to
the keyword argument key. Every time one element (here tuple) to the function. The
function returns key-th index number.
4. Print the result.

## Example

## list of tuples
tuples = [(2, 2), (1, 2), (3, 1)]
## key
key = 0
## function which returns the key-th index number from the tuple
def k_th_index(one_tuple):
return one_tuple[key]
## calling the sorted function
## pass the list of tuples as first argument
## give the function as a keyword argument to the **key**
sorted(tuples, key = k_th_index)

## Output

If you run the above program, you will get the following results.

[(1, 2), (2, 2), (3, 1)]

If you initialize the key with an index which is greater the len(tuple) - 1 then, you will get the index error. Let's see.

## Example

Live Demo

## list of tuples
tuples = [(2, 2), (1, 2), (3, 1)]
## key
## initializing the key which is greter than len(tuple) - 1
key = 2
## function which returns the key-th index number from the tuple
def k_th_index(one_tuple):
return one_tuple[key]
## calling the sorted function
## pass the list of tuples as first argument
## give the function as a keyword argument to the **key**
sorted(tuples, key = k_th_index)

## Output

If you run the above program, you will get the following results.

IndexError Traceback (most recent call last)
<ipython-input-13-4c3fa14880dd> in <module>
13 ## pass the list of tuples as first argument
14 ## give the function as a keyword argument to the **key**
---> 15 sorted(tuples, key = k_th_index)
<ipython-input-13-4c3fa14880dd> in k_th_index(one_tuple)
8 ## function which returns the key-th index number from the tuple
9 def k_th_index(one_tuple):
---> 10 return one_tuple[key]
11
12 ## calling the sorted function
IndexError: tuple index out of range

The above program will work for any number of tuples and any size of tuples until unless the index doesn't greater than len(tuple) - 1.

## Conclusion

I hope you enjoyed the tutorial. If you have any queries regarding the tutorial, please do mention them in the comment section.