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 ordered set of values enclosed in square brackets [].
Example:
list = [3 ,1, 5, 7]
List of tuples is a list whose each element is a tuple.
Example:
tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
Concatenating Maximum Tuples
We have a list of tuples with each tuple consisting of a string and an associated value. We need to concatenate the string values of the tuples with maximum associated values.
Input:
tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
Output:
"python programming"
Method 1:
A method to concatenate maximum value tuple string is by finding the tuples from the list with maximum value and then joining their string values.
To find max value, we will be using max() method and itemgetter to get value from tuple. Then using list comprehension we will find all values with value equal to max. And then using the join method we will concatenate the values.
Program:
Output:
Method 2:
An Alternative way to perform the task is using the filter method to separate max value string for joining to perform concatenation.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer