Python program to perform cross tuple summation grouping using 2nd element

Here, we have a list of tuples in which we need to perform cross tuple summation grouping using the 2nd element using the Python Program.
Submitted by Shivang Yadav, on September 11, 2021

In this program, we have a list of tuples. And we need to find cross tuple summation grouping using the second element of the tuple i.e. we need to create a tuple using two tuples with the same second element and the first element of our new tuple is the summation of elements of tuple with the common second element.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

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 an ordered set of values enclosed in square brackets []

Example:

list = [3 ,1,  5, 7]

Now, let's get back to our topic of cross tuple summation.

Cross Tuple Summation Grouping in Python

While creating complex applications we need to manipulate data structures a lot, one of which is a cross-collection summation in which we perform cross summation of collections based on the sum condition element.

Cross Tuple summation grouping is performing summation operation between tuples using a specific element of the tuple which in this problem is the 2nd element of the tuple. So, we will be adding the first elements of all tuples of the list whose second element is the same.

Input:
tupList = [(2, 3), (9, 3), (1, 5), (2, 8)]

Output:
[(11, 3), (1, 5), (2, 8)]

For solving such problems, python provides multiple ways and methods.

Method 1:

A direct method to solve the problem is by traversing the list and checking tuples with the same second element and performing addition based on it.

# Python program to perform cross tuple summation 

# Creating and printing the Tuple List 
tupList = [(2, 3), (9, 3), (1, 5), (2, 8)]
print("Initial Tuple List : " + str(tupList))

# cross tuple summing 
myDict = dict()
for val1, val2 in tupList:
	myDict[val2] = myDict.get(val2, 0) + val1
sumTuple = [(val2, val1) for (val1, val2) in myDict.items()]

# Printing summation list 
print(" List after cross Tuple summation : " + str(sumTuple))

Output:

Initial Tuple List : [(2, 3), (9, 3), (1, 5), (2, 8)]
 List after cross Tuple summation : [(11, 3), (1, 5), (2, 8)]

Method 2:

An Alternative method to solve the problem is by performing summation using sum() method along with list comprehension and checking for tuples with the same second value using groupby() method. Then create the list by using zip() method.

# Python program to perform cross tuple summation 

from itertools import groupby

# Creating and printing the Tuple List 
tupList = [(2, 3), (9, 3), (1, 5), (2, 8)]
print("Initial Tuple List : " + str(tupList))

# cross tuple summing 
sumTuple = [(sum(next(zip(*vals))), key) for key, vals in groupby(tupList, key = lambda tup:tup[1])]
  
# Printing summation list 
print(" List after cross Tuple summation : " + str(sumTuple))

Output:

Initial Tuple List : [(2, 3), (9, 3), (1, 5), (2, 8)]
 List after cross Tuple summation : [(11, 3), (1, 5), (2, 8)]

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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