Python Program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple

Tuple in Python:

Tuples, like Python lists, are a common data type that allows you to store values in a series. They could be handy in cases when you want to communicate data with someone but not enable them to change it. They can use the data values, but no change is reflected in the original data that was supplied.

Examples:

Example1:

Input:

 given list of tuples =[(5, 12, 98), (7, 1), (4, 19, 11, 9), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]

Output:

Printing the sorted list of tuples : 
[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]

Example2:

Input:

  given list of tuples =  [(7, 12, 23), (999, 4), (234, 245, 129), (10, 23, 456)]

Output:

Printing the sorted list of tuples : 
[(999, 4), (7, 12, 23), (234, 245, 129), (10, 23, 456)]

Example3:

Input:

   given list of tuples = [('hello', 'this'), ('BTechGeeks', 'online', 'platform'), ('for', 'students')]

Output:

Printing the sorted list of tuples : 
[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]

Program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple

There are several ways to sort a List of Tuples in ascending order by the last element in each tuple some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using sorted function

The Sorted() method sorts a list and always returns a list with the entries sorted without changing the original sequence.

i)List of tuples of Integer type

Approach:

  • Give the list of Tuples input as static input.
  • Create a function that returns the final element of each tuple in the list.
  • Create a new function with the previous function as the key, and sort the list using sorted() function.
  • Print the sorted list
  • Exit of Program

Below is the implementation:

def lastEle(ele):
  # returning the last element
    return ele[-1]


def sortlastElementTuple(listTuple):
  # using sorted function with first parameter as given list of tuples and
  # key as last element
  # To get last element we create a function which returns the last element
  # returning the sorted list of tuples
    return sorted(listTuple, key=lastEle)


listofTuples = [(5, 12, 98), (7, 1), (4, 19, 11, 9),
                (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]
# printing the sorted list of tuples by last element
print("Printing the sorted list of tuples : ")
# Passing the given list of tuples to sortlastElementTuple function
print(sortlastElementTuple(listofTuples))

Output:

Printing the sorted list of tuples : 
[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]

ii)List of tuples of String type

Below is the implementation:

def lastEle(ele):
  # returning the last element
    return ele[-1]


def sortlastElementTuple(listTuple):
  # using sorted function with first parameter as given list of tuples and
  # key as last element
  # To get last element we create a function which returns the last element
  # returning the sorted list of tuples
    return sorted(listTuple, key=lastEle)


listofTuples = [('hello', 'this'), ('BTechGeeks', 'online',
                                    'platform'), ('for', 'students')]
# printing the sorted list of tuples by last element
print("Printing the sorted list of tuples : ")
# Passing the given list of tuples to sortlastElementTuple function
print(sortlastElementTuple(listofTuples))

Output:

Printing the sorted list of tuples : 
[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]

Method #2:Using sort function

The sort() method arranges the items in a list in an ascending or descending order.

We use lambda function to get the last element in the given list of tuples.

i)List of tuples of Integer type

Below is the implementation:

def sortlastElementTuple(listTuple):
    # The key has been configured to sort using the
    # last element of the sublist lambda has been used
    listTuple.sort(key=lambda k: k[-1])
    # returning the sorted list of tuples
    return listTuple


listofTuples = [(5, 12, 98), (7, 1), (4, 19, 11, 9),
                (36, 82, 19, 1, 2, 5, 3, 6, 9, 6)]
# printing the sorted list of tuples by last element
print("Printing the sorted list of tuples : ")
# Passing the given list of tuples to sortlastElementTuple function
print(sortlastElementTuple(listofTuples))

Output:

Printing the sorted list of tuples : 
[(7, 1), (36, 82, 19, 1, 2, 5, 3, 6, 9, 6), (4, 19, 11, 9), (5, 12, 98)]

ii)List of tuples of String type

Below is the implementation:

def sortlastElementTuple(listTuple):
    # The key has been configured to sort using the
    # last element of the sublist lambda has been used
    listTuple.sort(key=lambda k: k[-1])
    # returning the sorted list of tuples
    return listTuple


listofTuples = [('hello', 'this'), ('BTechGeeks', 'online',
                                    'platform'), ('for', 'students')]
# printing the sorted list of tuples by last element
print("Printing the sorted list of tuples : ")
# Passing the given list of tuples to sortlastElementTuple function
print(sortlastElementTuple(listofTuples))

Output:

Printing the sorted list of tuples : 
[('BTechGeeks', 'online', 'platform'), ('for', 'students'), ('hello', 'this')]

Related Programs: