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:
Python | Tuple multiplication
Next article icon

Python | Tuple list cross multiplication

Last Updated : 09 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, while working with Python records, we can have a problem in which we need to perform cross multiplication of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + zip() The combination of the above functionalities can be used to perform this particular task. In this, we iterate through the list using list comprehension and the multiplication across lists is performed with the help of zip(). 

Python3




# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using list comprehension + zip()
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Tuple list cross multiplication
# using list comprehension + zip()
res = [(x[0] * y[0], x[1] * y[1]) for x, y in zip(test_list1, test_list2)]
 
# printing result
print("The multiplication across lists is : " + str(res))
 
 
Output : 
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]

Time complexity: O(n), where n is the length of the lists test_list1 and test_list2.

Auxiliary space: O(n), where n is the length of the lists test_list1 and test_list2, since the result is stored in a new list.

  Method #2 : Using loop + zip() + map() This is yet another way to perform this task. This is similar to above method, the difference is that multiplication is performed by explicit function and extending logic to each element is done by map(). 

Python3




# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using max() + zip() + loop
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Tuple list cross multiplication
# using max() + zip() + loop
res = [tuple(map(prod, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The multiplication across lists is : " + str(res))
 
 
Output : 
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]

The time complexity of this code is O(nm), where n is the length of test_list1 and m is the length of test_list2.

The space complexity of this code is O(n), where n is the length of test_list1. 

Method #3 : Using itertools.starmap()
The itertools module of Python provides us a starmap() function which is used to apply a given function to each of the tuple of an iterable. This can be used to perform this task.

Python3




# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using itertools.starmap()
   
# importing itertools for starmap()
import itertools
   
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
   
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
   
# Tuple list cross multiplication
# using itertools.starmap()
res = list(itertools.starmap(lambda x,y : (x[0] * y[0], x[1] * y[1]), zip(test_list1, test_list2)))
   
# printing result
print("The multiplication across lists is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]

Time Complexity: O(n) 
Space Complexity: O(n)

Method#4: using max() + zip() + recursion.

Python




# Python3 code to demonstrate working of
# Tuple list cross multiplication
# using max() + zip() + recursion
 
# getting Product
def prod(val) :
    if len(val) == 1:
        return val[0]
    return val[0] * prod(val[1:])
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Tuple list cross multiplication
# using max() + zip() + recursion
res = [tuple(map(prod, zip(a, b))) for a, b in zip(test_list1, test_list2)]
 
# printing result
print("The multiplication across lists is : " + str(res))
#this code contributed by tvsk
 
 
Output
The original list 1 : [(2, 4), (6, 7), (5, 1)] The original list 2 : [(5, 4), (8, 10), (8, 14)] The multiplication across lists is : [(10, 16), (48, 70), (40, 14)]

Time Complexity: O(n) 
Auxiliary Space: O(n)

Method #6: Using NumPy (without transposing)

Here’s another approach using NumPy, but without transposing the result. Instead, we can directly reshape the result array to get the desired output format.

Steps:

  1. Import the numpy library.
  2. Convert the given lists to numpy arrays using np.array().
  3. Use np.multiply() function to perform element-wise multiplication of arrays.
  4. Use np.reshape() function to reshape the result array to the desired format.

Python3




import numpy as np
 
# initialize lists
test_list1 = [(2, 4), (6, 7), (5, 1)]
test_list2 = [(5, 4), (8, 10), (8, 14)]
 
# convert lists to numpy arrays
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
 
# perform element-wise multiplication
res_arr = np.multiply(arr1, arr2)
 
# reshape the result array to tuple list format
res = res_arr.reshape(-1, 2).tolist()
 
# printing result
print("The multiplication across lists is : " + str(res))
 
 
OUTPUT: The multiplication across lists is : [[10, 16], [48, 70], [40, 14]]

Time Complexity: O(n)
Auxiliary Space: O(n)



Next Article
Python | Tuple multiplication
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Tuple multiplication
    Sometimes, while working with records, we can have a problem in which we may need to perform multiplication of 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 The combination of abov
    5 min read
  • Python - Cross Pairing in Tuple List
    Given 2 tuples, perform cross pairing of corresponding tuples, convert to single tuple if 1st element of both tuple matches. Input : test_list1 = [(1, 7), (6, 7), (8, 100), (4, 21)], test_list2 = [(1, 3), (2, 1), (9, 7), (2, 17)] Output : [(7, 3)] Explanation : 1 occurs as tuple element at pos. 1 in
    5 min read
  • Python - Constant Multiplication over List
    We are given a list we need to multiply each element in the list by a constant. For example, we are having a list a = [1, 2, 3, 4, 5] and constant c=2 we need to multiply this constant in whole list. Using List ComprehensionList comprehension allows us to multiply each element in list by a constant
    3 min read
  • Python - List of tuples to multiple lists
    Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data. Using zip()zip() function is a concise
    3 min read
  • Python | Custom Multiplication in list of lists
    Sometimes, when we are fed with the list of list, we need to multiply each of its element list with a particular element fed by the order in the other list. This particular problem is very specific but knowledge of it can be useful in such cases. Let's discuss certain ways in which this can be done.
    8 min read
  • Python - Constant Multiplication to Nth Column
    Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let’s discuss certain ways in which K can be multiplied to Nth element of tuple in list. Method #1 : Using loop Using loops this t
    7 min read
  • Python | Convert list of tuples into list
    In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
    3 min read
  • tuple() Constructor in Python
    In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list,
    3 min read
  • Convert List of Tuples To Multiple Lists in Python
    When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
    4 min read
  • Python - Unique values Multiplication
    This article focuses on one of the operation of getting the unique list from a list that contains a possible duplicates and performing its product. This operations has large no. of applications and hence it’s knowledge is good to have. Method 1 : Naive method + loop In naive method, we simply traver
    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