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.
By Shivang Yadav Last updated : December 12, 2023

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.

Example

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: Using max() method

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: Using filter() method

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 »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.