- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Addition of tuples in Python
When it is required to add the tuples, the 'amp' and lambda functions can be used.
The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.
Anonymous function is a function which is defined without a name.
In general, functions in Python are defined using 'def' keyword, but anonymous function is defined with the help of 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the result of it. Below is a demonstration of the same −
my_tuple_1 = (11, 14, 54, 56, 87) my_tuple_2 = (98, 0, 10, 13, 76) print("The first tuple is : ") print(my_tuple_1) print("The second tuple is : ") print(my_tuple_2) my_result = tuple(map(lambda i, j: i + j, my_tuple_1, my_tuple_2)) print("The tuple after addition is: ") print(my_result)
The first tuple is : (11, 14, 54, 56, 87) The second tuple is : (98, 0, 10, 13, 76) The tuple after addition is: (109, 14, 64, 69, 163)
- Two tuples are defined, and are displayed on the console.
- The lambda function is applied on every element of both the tuples, and the 'map' method is used to map the process of addition.
- It is then converted to a tuple.
- This is assigned to a value.
- It is displayed on the console.
- Related Articles
- Pairwise Addition in Tuples in Python
- Addition in Nested Tuples in Python
- Combining tuples in list of tuples in Python
- Count tuples occurrence in list of tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Accessing Values of Tuples in Python
- Summation of tuples in list in Python
- Remove tuples from list of tuples if greater than n in Python
- Python program to find Tuples with positive elements in List of tuples
- Updating Tuples in Python
- Compare tuples in Python
- Python - Column summation of tuples
- How to Concatenate tuples to nested tuples in Python
- Remove Tuples of Length K in Python
- Concatenation of two String Tuples in Python