How to Sort Tuple in Python
Tuples are a type of data type of a variable that lets us store multiple objects in one place. A tuple is an ordered and immutable (we cannot update elements in a tuple) collection of items. There are 4 in-built Python data structures to store elements, one of them is a tuple, and the others are List, Dictionary, and Set, each with its own set of properties and use. They are written in round brackets.
Sorting a Tuple in Python
The sort() method is often used to sort elements of a list in ascending order, with the first element being sorted by default. We can sort a tuple by converting it to a list first and then apply this function. This function sorts the list in place and returns None.
('Itika', 'Arshia', 'Peter', 'Parker') <class 'tuple'>
In Python, use the sorted() built-in function to sort a Tuple. The tuple should be passed as an argument to the sorted() function. The tuple items are sorted (by default) in ascending order in the list returned by the function. We can use a tuple to convert this list data type to a tuple ().
The reverse parameter to the sorted() function can also specify the sorting order. Ascending is the default sorting order. The items are sorted in descending order when reverse=True is set. We can also specify a key function whose returned values are used to sort items. We take a tuple, tuple_, having integer values, and sort it in ascending order in the following program.
Sorted Tuple : (1, 2, 3, 5, 6, 7, 24) <class 'tuple'>
Now we are sorting the tuple in descending order using same function. Pass reverse=True to the sorted() function to sort the tuple in descending order.
Sorted Tuple : (24, 7, 6, 5, 3, 2, 1) <class 'tuple'>
Sorting a Tuple Based on a Key Function
A key is a function that takes a value and returns a value. For each of the items in tuple, this key function is applied, and the returned value is used for comparison to sort the items. In the following program, we sort tuple of strings based on the length of strings. For this case, we may use len() builtin function as key.
Sorted Tuple : ('sjs', 'abhd', 'sxshs', 'sbchcwsc') <class 'tuple'>
Sorting List of Tuples
Let's look at how to sort a list in Python using a tuple. Consider the following scenario: we wish to sort the list of tuples. We must sort tuples according to any key given to us. This can be accomplished using the sorted() function, which sorts items using a key and stores the key index for sorting the given tuples. The Python execution of this approach is as follows:
Sorted list of Tuples: [(27, 11, 32), (24, 12, 32), (34, 21, 56), (42, 34, 42)]
Using Bubble Sort
Bubble sort is just a sorting algorithm for sorting a list of any number of elements. If the adjoining item in a given list are in the incorrect order, it swaps them. It then repeats this process until all of the elements have been sorted.
In this example, we'll use the bubble sort algorithm to sort a list of tuples.
[('Arshia', 26), ('Itika', 53), ('MJ', 45), ('Parker', 74), ('Peter', 82)]
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week