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 - Product of i^k in List
Next article icon

Python | Product of kth column in List of Lists

Last Updated : 04 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop + zip() This task can be solved using the combination of above functions. In this, we pass in the zip() the list, to access all columns and explicit product function to get product of columns. 

Python3




# Python3 code to demonstrate working of
# Column Product in List of Lists
# using loop + zip()
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res
     
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize K
K = 2
 
# Column Product in List of Lists
# using loop + zip()
res = [prod(idx) for idx in zip(*test_list)][K]
 
# printing result
print("Product of Kth column is : " + str(res))
 
 
Output : 
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Product of Kth column is : 56

Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.

Method #2 : Using loop This is brute force way to solve this problem. In this, we iterate through the matrix and take product of column by testing column number. 

Python3




# Python3 code to demonstrate working of
# Column Product in List of Lists
# Using loop
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize K
K = 2
 
# Column Product in List of Lists
# Using loop
res = 1
for sub in test_list:
    for idx in range(0, len(sub)):
        if idx == K:
            res = res * sub[idx]
 
# printing result
print("Product of Kth column is : " + str(res))
 
 
Output : 
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Product of Kth column is : 56

Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.

Method #3 : Using numpy
Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
   
# printing original list
print("The original list is : ",test_list)
 
# initialize K
K = 2
   
# Column Product in List of Lists
# Using numpy
res = np.prod(np.array(test_list)[:, K])
 
# printing result
print("Product of Kth column is : ",res)
#This code is contributed by Edula Vinay Kumar Reddy
 
 

Output:

The original list is :  [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Product of Kth column is :  56

The time complexity of this approach is O(n) where n is the number of elements in the matrix. The auxiliary space is O(1) as we are not using any extra space.

Method 4 : use list comprehension along with the built-in function reduce() from the functools module.

Step-by-step approach:

  • Import the functools module.
  • Define the list of lists to be processed.
  • Set the value of K to the desired column index.
  • Use a list comprehension to extract the Kth element from each sublist in the main list and create a new list.
  • Use the reduce() function from the functools module to multiply all the elements in the new list together.
  • Print the result.

Python3




import functools
 
# initialize list
test_list = [[5, 6, 7],
            [9, 10, 2],
            [10, 3, 4]]
   
# printing original list
print("The original list is : ", test_list)
 
# initialize K
K = 2
 
# Using list comprehension and reduce()
res = functools.reduce(lambda x, y: x * y, [sublist[K] for sublist in test_list])
 
# printing result
print("Product of Kth column is : ", res)
 
 
Output
The original list is :  [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Product of Kth column is :  56

The time complexity of this method is O(N), where N is the total number of elements in the list of lists.
The auxiliary space complexity is also O(N), as we create a new list of size N to store the Kth elements from each sublist.



Next Article
Python - Product of i^k in List
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python-list-of-lists
Practice Tags :
  • python

Similar Reads

  • Python | Column Product in List of lists
    Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Let’s discuss certain ways in which this problem can be solved. Meth
    6 min read
  • Python - Kth Column Product in Tuple List
    Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the product of it’s Kth index. This problem has application in web development domain while working with data informations. Let’s discuss certain ways in which this task can be performed. M
    7 min read
  • Python - Product of i^k in List
    Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of i^k of list in just one line. Let’s discuss
    5 min read
  • Python | Product of Prefix in list
    Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Let’s discuss certain ways in which this problem can be solved. Method 1: Using list comprehen
    4 min read
  • Iterate Over a List of Lists in Python
    We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
    2 min read
  • Python | Nth column Matrix Product
    Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using
    7 min read
  • Python - Cubes Product in list
    Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding product of cubes of list in just one line. Let’s discu
    5 min read
  • Python - Product of consecutive pairs in list
    Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved.
    7 min read
  • Flatten a List of Lists in Python
    Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python. Using itertools.chain itertools modu
    3 min read
  • Python - Product of elements using Index list
    Accessing an element from its index is easier task in python, just using the [] operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices and then perform the multipl
    7 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