Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Tuple Division in Python
Next article icon

Python – Pairwise Addition in Tuples

Last Updated : 28 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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




# Python3 code to demonstrate working of
# Pairwise Addition in Tuples
# using zip() + generator expression + tuple
 
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Pairwise Addition in Tuples
# using zip() + generator expression + tuple
res = tuple(i + j for i, j in zip(test_tup, test_tup[1:]))
 
# printing result
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




# Python3 code to demonstrate working of
# Pairwise Addition in Tuples
# using tuple() + map() + lambda
 
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Pairwise Addition in Tuples
# using tuple() + map() + lambda
res = tuple(map(lambda i, j : i + j, test_tup[1:], test_tup[:-1]))
 
# printing result
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
 
#initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
#print original tuple
print("The original tuple : " + str(test_tup))
 
#Pairwise Addition in Tuples using numpy
res = np.add(test_tup[1:], test_tup[:-1])
 
#print resultant tuple
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 

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




#initialize tuple
test_tup = (1, 5, 7, 8, 10)
#printing original tuple
print("The original tuple : " + str(test_tup))
#pairwise addition
res = tuple([test_tup[i]+test_tup[i+1] for i in range(len(test_tup)-1)])
#printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by pinjala Jyothi
 
 
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

  1. Initialize an empty list res.
  2. Iterate over the enumerated test_tup starting from the second element.
  3. For each element, add the current element and the previous element and append it to the res list.
  4. Convert the res list to a tuple and assign it to the variable res.
  5. Print the resultant tuple.

Python3




#initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
#printing original tuple
print("The original tuple : " + str(test_tup))
 
#pairwise addition using enumeration
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])
 
#printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Vinay Pinjala.
 
 
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.



Next Article
Tuple Division in Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

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
  • Tuple Division in Python
    Sometimes, while working with records, we can have a problem in which we may need to perform mathematical division operation across tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression Th
    6 min read
  • Python | Add dictionary to tuple
    Sometimes, while working with data, we can have a problem in which we need to append to a tuple a new record which is of form of Python dictionary. This kind of application can come in web development domain in case of composite attributes. Let's discuss certain ways in which this task can be perfor
    4 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
  • 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
  • Python | Index Maximum among Tuples
    Sometimes, while working with records, we might have a common problem of maximizing contents of one tuple with 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
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences