ADVERTISEMENT
ADVERTISEMENT

Python program to concatenate maximum tuples

Here, we have a list of tuples and we need to concatenate the maximum tuples in Python programming language.
Submitted by Shivang Yadav, on June 23, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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"

To perform maximum concatenation, we need to find all maximum value tuples and then concatenate their string values. Python provides multiple options to perform the task. Let’s discuss a few of them,

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

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.