Q:

Python program to concatenate maximum tuples

belongs to collection: Python Tuple Programs

0

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"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

# Python Program to concatenate Maximum Tuples
from operator import itemgetter

# Creating and printing list of tuples 
tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
print("List of Tuples : " + str(tupList))

# Concatenating maximum tuple 
maxVal = max(tupList, key=itemgetter(1))[1]
concString = ' '.join([key for key, ele in tupList if ele == maxVal])

# Printing concatenated string 
print("Maximum concatenated string from list : " + str(concString))

Output:

List of Tuples : [('python', 7), ('learn', 1), ('programming', 7), ('code', 3)]
Maximum concatenated string from list : python programming

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:

# Python Program to concatenate Maximum Tuples
from operator import itemgetter

# Creating and printing list of tuples 
tupList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]
print("List of Tuples : " + str(tupList))

# Concatenating maximum tuple 
maxVal = max(tupList, key=itemgetter(1))[1]
concString = " ".join([ele[0] for ele in filter(lambda ele: ele[1] == maxVal, tupList)])

# Printing concatenated string 
print("Maximum concatenated string from list : " + str(concString))

Output:

List of Tuples : [('python', 7), ('learn', 1), ('programming', 7), ('code', 3)]
Maximum concatenated string from list : python programming

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to flatten tuple of lists to a tupl... >>
<< Python program to order tuples by list...