How to add tuples by element by element in python? Tuple in Python is a fundamental data structure that allows you to store multiple values in a single object. A tuple is an ordered and immutable (cannot update elements in a tuple) collection of items.
You can perform two tuples element by element by using many ways, for example, using the for loop, map()+lambda, map()+zip()+sum(), and numpy.add() functions. In this article, I will explain the addition of tuples using all these methods with examples.
1. Quick Examples of Add Elements of Tuples
If you are in a hurry, below are some quick examples of how to add the elements of the tuple.
# Quick examples of tuple addition
# Example 1: Addition of tuples
# Using map() + lambda
tuples1 = (5, 10, 15, 20)
tuples2 = (3, 5, 7 ,9)
result = tuple(map(lambda x, y: x + y, tuples1, tuples2))
# Example 2: Using tuple() method and for loop
tuples1 = (15, 7, 10, 20)
tuples2 = (5, 8, 12, 4)
tup=[]
for i in range(0,len(tuples1)):
tup.append(tuples1[i]+tuples2[i])
result =tuple(tup)
# Example 3: Addition of tuples
# Using map() + zip() + sum()
tuples1 = (13, 16, 15, 10)
tuples2 = (5, 8, 12, 9)
tuples3 = (8, 4, 6, 3)
result = tuple(map(sum, zip(tuples1, tuples2, tuples3)))
# Example 4: Add the two arrays
# Using NumPy add function
# Convert tuples to NumPy arrays
arr1 = np.array((10, 17, 28, 20))
arr2 = np.array((15, 9, 12, 18))
result_arr = np.add(arr1, arr2)
result = tuple(result_arr)
2. Add Tuples Using map() + Lambda
You can add two or multiple tuples by element-wise using the map() function and a lambda function. For example, you first define two tuples tuples1 and tuples2 with four elements each, use the map() to apply a lambda function to each pair of corresponding elements from the two tuples.
The lambda function takes two arguments x and y, which correspond to the elements from tuples1 and tuples2, respectively. It then returns the sum of x and y.
# Initialize tuples
tuples1 = (5, 10, 15, 20)
tuples2 = (3, 5, 7 ,9)
print(“Actual tuple1: “,tuples1)
print(“Actual tuple2: “,tuples2)
# Addition of tuples
# using map() + lambda
result = tuple(map(lambda x, y: x + y, tuples1, tuples2))
print(“Updated tuple addition: “,result)
# Output
# Actual tuple1: (5, 10, 15, 20)
# Actual tuple2: (3, 5, 7, 9)
# Updated tuple addition: (8, 15, 22, 29)
3. Addition of Tuple Using tuple() Method and For Loop
Let’s see adding two tuples using a for loop and the tuple() method to create a new tuple from the sum of each corresponding element in the original tuples. For example, you first define two tuples tuples1 and tuples2 with four elements each. An empty list tup is initialized to store the sum of each corresponding element from tuples1 and tuples2.
Use a for loop to iterate over the indices of the tuples. At each index, add the corresponding elements from tuples1 and tuples2 are added together and appended to the tup list. Convert the tup list to a tuple using the tuple() method.
# initialize tuples
tuples1 = (15, 7, 10, 20)
tuples2 = (5, 8, 12, 4)
print(“Actual tuple1: “,tuples1)
print(“Actual tuple2: “,tuples2)
# Addition of tuples
tup=[]
for i in range(0,len(tuples1)):
tup.append(tuples1[i]+tuples2[i])
result =tuple(tup)
print(“Updated tuple addition: “,result)
# Output
# Actual tuple1: (15, 7, 10, 20)
# Actual tuple2: (5, 8, 12, 4)
# Updated tuple addition: (20, 15, 22, 24)
4. Addition of Tuple Using map() + zip() + sum()
To add corresponding elements of multiple tuples together, you can use the map(), zip(), and sum() functions in Python. For example, zip() function takes multiple tuples and returns an iterator that contains tuples of corresponding elements from all the tuples. Then map() function applies sum() function to each tuple of corresponding elements, resulting in an iterator of the sums of corresponding elements.
# initialize tuples
tuples1 = (13, 16, 15, 10)
tuples2 = (5, 8, 12, 9)
tuples3 = (8, 4, 6, 3)
print(“Actual tuple1: “,tuples1)
print(“Actual tuple2: “,tuples2)
print(“Actual tuple3: “,tuples3)
# Addition of tuples
# Using map() + zip() + sum()
result = tuple(map(sum, zip(tuples1, tuples2, tuples3)))
print(“Updated tuple addition: “,result)
# Output
# Actual tuple1: (13, 16, 15, 10)
# Actual tuple2: (5, 8, 12, 9)
# Actual tuple3: (8, 4, 6, 3)
# Updated tuple addition: (26, 28, 33, 22)
5. Addition of Tuple Using NumPy
Similarly, to add two tuples you can also use NumPy. To use this first convert them to NumPy arrays and then use the numpy.add() function. For example, first defined two tuples tuple1 and tuple2, converted them to NumPy arrays using the numpy.array() function. You can use the numpy.add() function to add the two arrays element-wise, resulting in a new NumPy array result_arr.
import numpy as np
# initialize tuples
tuples1 = (10, 17, 28, 20)
tuples2 = (15, 9, 12, 18)
# convert tuples to NumPy arrays
arr1 = np.array(tuples1)
arr2 = np.array(tuples2)
# add the two arrays using NumPy add function
result_arr = np.add(arr1, arr2)
result = tuple(result_arr)
print(result)
# Output
# (25, 26, 40, 38)
Conclusion
In this article, I have explained how to add two or multiple tuples in python by using for loop, map()+Lambda, map()+zip()+sum(), and numpy.add() function with examples.
Happy Learning !!
How to add tuples by element by element in python? Tuple in Python is a fundamental data structure that allows you to store multiple values in a single object. A tuple is an ordered and immutable (cannot update elements in a tuple) collection of items. You can perform two tuples element by element by using Read More Python, Python Tutorial, python tuple examples