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 - Get Nth column elements in Tuple Strings
Next article icon

Python – Count elements in record tuple

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

Sometimes, while working with data in form of records, we can have a problem in which we need to find the total element counts of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed.

Method #1: Using len() + generator expression 

This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the count of elements using len(). 

Python3




# Python3 code to demonstrate working of
# Record elements count
# using len() + generator expression
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Record elements count
# using len() + generator expression
res = len(list((int(j) for i in test_list for j in i)))
 
# printing result
print("The total count of list is : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The total count of list is : 10

Time Complexity: O(n) where n is the number of elements in the list “test_list”. len() + generator expression performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #2 : Using len() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of finding total count is done by the combination of map() and from_iterable(). 

Python3




# Python3 code to demonstrate working of
# Record elements count
# using len() + map() + chain.from_iterable()
from itertools import chain
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Record elements count
# using len() + map() + chain.from_iterable()
res = len(list((map(int, chain.from_iterable(test_list)))))
 
# printing result
print("The total count of list is : " + str(res))
 
 
Output : 
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The total count of list is : 10

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using len() + map() + chain.from_iterable() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space 

Method #3 : Using extend(),list(),len() methods

Python3




# Python3 code to demonstrate working of
# Record elements count
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
res=[]
for i in test_list:
    res.extend(list(i))
# printing result
print("The total count of list is : " + str(len(res)))
 
 
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The total count of list is : 10

Time complexity: O(n), where n is the length of the test_list. 
Auxiliary Space: O(n), where n is the length of the test_list. 

Method 4: Using sum() and map()
 

Python3




# Python3 code to demonstrate working of
# Record elements count
# using sum() and map()
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Record elements count
# using sum() and map()
res = sum(map(len, test_list))
 
# printing result
print("The total count of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)] The total count of list is : 10

In this method, we use the built-in sum() function and map() function to iterate over the list and get the length of each tuple, and finally sum up all the lengths using the sum() function.

Time Complexity: O(n) where n is the number of elements in the list
Space Complexity: O(1) as we are only storing a single variable res

METHOD 5:Using loop.

APPROACH:

This program counts the total number of elements present in a list of tuples.

ALGORITHM:

1. Initialize a variable count to 0
2. Loop through each tuple in the list of tuples list1
3. Add the length of each tuple to the count variable
4. Print the value of the count variable

Python3




list1 = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
count = 0
for tup in list1:
    count += len(tup)
 
print(f"The total count of list is: {count}")
 
 
Output
The total count of list is: 10

Time Complexity: O(n), where n is the total number of tuples in the list
Space Complexity: O(1), as only one extra variable (count) is used. The space used by the input list is not counted as extra space.



Next Article
Python - Get Nth column elements in Tuple Strings
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Count elements in tuple list
    Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed. Method #1: Using len()
    5 min read
  • Python | Counting Nth tuple element
    Sometimes, while working with Python, we can have a problem in which we need to count the occurrence of a particular's elements. This kind of problem is quite common while working with records. Let's discuss a way in which this task can be performed. Method #1 : Using Counter() + generator expressio
    5 min read
  • Python | Elementwise AND in tuples
    Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise AND 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
    5 min read
  • Python | Extend tuples by count of elements in tuple
    Sometimes, while working with data, we can have an application in which we need to duplicate tuple elements by the amount of element count. This is very unique application but can occur in certain cases. Let's discuss certain ways in which this task can be performed. Method #1: Using nested loops Th
    6 min read
  • Python - Get Nth column elements in Tuple Strings
    Yet another peculiar problem that might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This article solves the problem of extracting only the
    8 min read
  • Python | Tuple Column element frequency
    In Python, we need to handle various forms of data and one among them is a list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the Kth element in the list of tuples. Let’s discuss certain ways in which this can
    5 min read
  • Python | Matching elements count
    Sometimes, while working with lists we need to handle two lists and search for the matches, and return just the count of indices of the match. Querying whole list for the this process is not feasible when the size of master list is very large, hence having just the match indices helps in this cause.
    5 min read
  • Python - Filter consecutive elements Tuples
    Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
    5 min read
  • Python - Remove Consecutive K element records
    Sometimes, while working with Python records, we can have a problem in which we need to remove records on the basis of presence of consecutive K elements in tuple. This kind of problem is peculiar but can have applications in data domains. Let's discuss certain ways in which this task can be perform
    7 min read
  • Python Tuple count() Method
    In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example [GFGTABS] Python3 tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) [/GFGTABS] Output : 3Python Tuple c
    3 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