Python – Pairwise Addition in Tuples
Last Updated :
28 Feb, 2023
Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element addition. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using zip() + generator expression + tuple() The combination of above functionalities can be used to perform this task. In this, we use generator expression to provide addition logic and simultaneous element pairing is done by zip(). The result is converted to tuple form using tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ("The original tuple : " + str (test_tup))
res = tuple (i + j for i, j in zip (test_tup, test_tup[ 1 :]))
print ("Resultant tuple after addition : " + str (res))
|
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time complexity: O(n), where n is the length of the input tuple test_tup. This is because the program iterates through the test_tup tuple once to create the res tuple, which has a length of n-1 due to the use of test_tup[1:] in the zip() function.
Auxiliary space: O(n), because the program creates a new tuple res with a length of n-1 to store the pairwise additions of the input tuple test_tup
Method #2 : Using tuple() + map() + lambda The combination of above functions can also help to perform this task. In this, we perform addition and binding logic using lambda function. The map() is used to iterate to each element and at end result is converted by tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ("The original tuple : " + str (test_tup))
res = tuple ( map ( lambda i, j : i + j, test_tup[ 1 :], test_tup[: - 1 ]))
print ("Resultant tuple after addition : " + str (res))
|
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Method #3: Using numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = np.add(test_tup[ 1 :], test_tup[: - 1 ])
print ( "Resultant tuple after addition : " + str (res))
|
Output:
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : [ 6 12 15 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using list comprehension:
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ([test_tup[i] + test_tup[i + 1 ] for i in range ( len (test_tup) - 1 )])
print ( "Resultant tuple after addition : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using enumeration
- Initialize an empty list res.
- Iterate over the enumerated test_tup starting from the second element.
- For each element, add the current element and the previous element and append it to the res list.
- Convert the res list to a tuple and assign it to the variable res.
- Print the resultant tuple.
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ([x + y for i, x in enumerate (test_tup) if i < len (test_tup) - 1 for j, y in enumerate (test_tup) if j = = i + 1 ])
print ( "Resultant tuple after addition : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time Complexity: O(n), where n is the length of the input tuple. We need to iterate over the entire input tuple once to generate the resultant tuple.
Auxiliary Space: O(n), where n is the length of the input tuple. We need to store the resultant tuple in memory.
Similar Reads
Addition in Nested Tuples - Python
Sometimes, while working with records, we can have a problem in which we require to perform index wise addition of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let's discuss certain ways in which this problem can be solved. Method #1: Us
6 min read
Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let's discuss certain ways in which this task can be performed. Metho
5 min read
Python - Row-wise element Addition in Tuple Matrix
Sometimes, while working with Python tuples, we can have a problem in which we need to perform Row-wise custom elements addition in Tuple matrix. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [[('Gfg', 3
4 min read
Python - All pair combinations of 2 tuples
Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let's discuss certain ways in which this task can be performed. Input : test_tuple1
6 min read
Python | All possible N combination tuples
Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + product() T
5 min read
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ]. Using print()print() function is the
2 min read
Python | Test if tuple is distinct
Sometimes, while working with records, we have a problem in which we need to find if all elements of tuple are different. This can have applications in many domains including web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is a brute force
4 min read
Python | Compare tuples
Sometimes, while working with records, we can have a problem in which we need to check if each element of one tuple is greater/smaller than it's corresponding index in other tuple. This can have many possible applications. Let's discuss certain ways in which this task can be performed. Method #1 : U
4 min read
Add a key value pair to Dictionary in Python
The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key. For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'f
3 min read
Python List and Tuple Combination Programs
Lists and tuples are two of the most commonly used data structures in Python. While lists are mutable and allow modifications, tuples are immutable and provide a stable structure for storing data. This article explores various programs related to list and tuple combinations, covering topics like: So
6 min read