Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets [].
list = [3 ,1, 5, 7]
List of tuples is a list whose each element is a tuple.
tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
Sorting Tuples by Total Digits
We have a list of tuples with multiple values. And we need to sort the tuples of the list by the total number of digits in the tuple.
Input:
[(2, 4), (12, 40), (1, 23), (43343, 1)]
Output:
[(2, 4), (1, 23), (12, 40), (43343, 1)]
Method 1:
A method to solve the problem is by sorting the list using the sort() method with the sorting key is the sum of lengths of all elements of the tuple.
Output:
Method 2:
One method to perform the task is using the lambda function instead of passing a method to the tuple. The lambda will take the list and sort it based on the count of digits in the tuple.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer