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:
Output of python program | Set 12(Lists and Tuples)
Next article icon

Position Summation in List of Tuples – Python

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Position Summation in List of Tuples refers to the process of calculating the sum of elements at the same positions across multiple tuples in a list. This operation involves adding up the corresponding elements from each tuple.

For example, consider the list of tuples [(1, 6), (3, 4), (5, 8)]. The goal is to sum all the first elements together and all the second elements together. The result of this position summation would be (9, 18), where 9 is the sum of the first elements (1 + 3 + 5) and 18 is the sum of the second elements (6 + 4 + 8).

Using zip()

zip function when used with unpacking (*) groups elements from each tuple based on their respective positions. By using the map function with sum, we calculate the sum of each grouped tuple efficiently in a single pass.

Python
li = [(1, 6), (3, 4), (5, 8)] res = tuple(map(sum, zip(*li))) print(str(res)) 

Output
(9, 18) 

Explanation:

  • zip(*li): * operator unpacks li into individual tuples and zip then groups elements at the same positions .
  • map(sum, …): This applies sum to each group of li .
  • tuple(): This converts the result into a tuple .

Table of Content

  • Using Numpy arrays
  • Using reduce
  • Using for loop

Using Numpy arrays

For large datasets, NumPy provides highly optimized array operations that significantly improve performance. By converting a list of tuples into a NumPy array and using the np.sum() function we can efficiently compute position wise summation.

Python
import numpy as np li = [(1, 6), (3, 4), (5, 8)] res = tuple(np.sum(np.array(li), axis=0)) print(str(res)) 

Output
(np.int64(9), np.int64(18)) 

Explanation:

  • np.array(li) converts the list of tuples li into a NumPy array, enabling efficient numerical operations.
  • np.sum() This sums elements along the columns .
  • tuple(np.sum(…)) This converts the result from NumPy array to a Python tuple.

Using reduce

reduce () from functools efficiently performs pairwise computation across tuples by applying a summation to aggregate values. It directly combines results during iteration, avoiding the need for intermediate structures and making it ideal for large datasets.

Python
from functools import reduce li= [(1, 6), (3, 4), (5, 8)] res = reduce(lambda acc, ele: (acc[0] + ele[0], acc[1] + ele[1]),li) print(str(res)) 

Output
(9, 18) 

Explanation:

  • reduce: This applies the lambda function cumulatively to the list and starting with the first tuple.
  • lambda function: This sums the first elements and second elements separately across all tuples.

Using for loop

Using a for loop for position wise summation is simple but it may be slower for large datasets due to Python’s interpreted nature, lacking the optimizations of methods like NumPy or reduce. It’s efficient for smaller datasets but less so for larger ones.

Python
li = [(1, 6), (3, 4), (5, 8)]  # Manual summation sum1, sum2 = 0, 0 for x, y in li:     sum1 += x     sum2 += y  res = (sum1, sum2) print(str(res)) 

Output
(9, 18) 

Explanation:

  • for x, y in li: This loops through each tuple, unpacking it into x and y.
  • sum1 += x: This adds x to sum1.
  • sum2 += y: This adds y to sum2.


Next Article
Output of python program | Set 12(Lists and Tuples)
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python list-programs
  • python-list
  • python-tuple
Practice Tags :
  • python
  • python-list

Similar Reads

  • Python | sort list of tuple based on sum
    Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple. Examples: Input: [(4, 5), (2, 3), (6, 7), (2, 8)] Output: [(2, 3), (4, 5), (2, 8), (6, 7)] Input: [(3, 4), (7, 8), (6, 5)] Output: [(3, 4), (6, 5), (7, 8)] # Method 1: Using bubble sort Using th
    4 min read
  • Output of python program | Set 12(Lists and Tuples)
    Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? C/C++ Code L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11
    3 min read
  • How to iterate through list of tuples in Python
    In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Let’s explore these
    2 min read
  • Ways to Iterate Tuple List of Lists - Python
    In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi
    3 min read
  • Create a List of Tuples in Python
    The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
    3 min read
  • Convert Tuple to List in Python
    In Python, tuples and lists are commonly used data structures, but they have different properties: Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for fur
    2 min read
  • How to Take a Tuple as an Input in Python?
    Tuples are immutable data structures in Python making them ideal for storing fixed collections of items. In many situations, you may need to take a tuple as input from the user. Let's explore different methods to input tuples in Python. The simplest way to take a tuple as input is by using the split
    3 min read
  • Python | Set 3 (Strings, Lists, Tuples, Iterations)
    In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
    3 min read
  • Output of Python Programs | Set 20 (Tuples)
    Prerequisite: Tuples Note: The output of all these programs is tested on Python3 1. What will be the output of the following program? [GFGTABS] Python3 tuple = (1, 2, 3, 4) tuple.append( (5, 6, 7) ) print(len(tuple)) [/GFGTABS]Options: 125Error Output: 4. ErrorExplanation: In this case an exception
    2 min read
  • Transpose Dual Tuple List in Python
    Sometimes, while working with Python tuples, we can have a problem in which we need to perform tuple transpose of elements i.e, each column element of dual tuple becomes a row, a 2*N Tuple becomes N * 2 Tuple List. This kind of problem can have possible applications in domains such as web developmen
    5 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