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 | Find number of lists in a tuple
Next article icon

How to Get the Number of Elements in a Python List

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, lists are one of the most commonly used data structures to store an ordered collection of items.

In this article, we’ll learn how to find the number of elements in a given list with different methods.

Example

Input: [1, 2, 3.5, geeks, for, geeks, -11]
Output: 7

Let’s explore various ways to doing it:

Using len() function

The simplest and most efficient way is to use Python’s built-in len() function.

Python
a = [1, 2, 3, 4]  print(len(a)) 

Output
4 

Explanation: len(a) returns the total number of items in the list a.

Using operator.length_hint()

The length_hint() function from the operator module gives an estimated length of a collection.

Python
from operator import length_hint  a = [1, 2, 3, 4]  print(length_hint(a)) 

Output
4 

Explanation:

  • The code imports the length_hint function from the operator module.
  • length_hint(a) returns the number of elements present in the list a which is 4. .

Using for loop

We can declare a counter variable to count the number of elements in the list while iterating the list using a for loop.

Python
a = [1, 2, 3, 4] count = 0  for _ in a:     count += 1  print(count) 

Output
4 

Explanation: We initialize count as 0 and increment it for every element in the list.

Using Numpy Library

We can also use the popular NumPy library’s size() function.

Python
import numpy as np   a = [1, 2, 3, 4]   print(np.size(a))  

Output
4 

Explanation: np.size(a) returns the total number of elements in the array or list.



Next Article
Python | Find number of lists in a tuple
author
phanitejau
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Get the Last Element of List in Python
    In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list: Input: list = [1, 3, 34, 12, 6]Output: 6 Explanation: Last element of the list l in the above example is 6. Let's explore various methods of doing it in Python: 1. Using
    2 min read
  • Python | Find number of lists in a tuple
    Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application. Method #1: Using len C/C++ Code # Python code to find number of list in a tuple # Initial list Input1 = ([1, 2, 3, 4], [5, 6, 7, 8]) Input2 =
    4 min read
  • Append Elements to Empty List in Python
    In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
    2 min read
  • Python - Find the frequency of numbers greater than each element in a list
    Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3,
    8 min read
  • Python - Step Frequency of elements in List
    Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
    4 min read
  • Get the indices of all occurrences of an element in a list - Python
    We are given a list and our task is to find all the indices where a particular element occurs. For example, if we have a list like [1, 2, 3, 2, 4, 2] and the element is 2, then the output will be [1, 3, 5] because 2 appears at these positions. Using List ComprehensionList comprehension allows for a
    2 min read
  • Counting number of unique values in a Python list
    Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates. Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once. [GFGTABS] Pyt
    2 min read
  • Python | Check if any element occurs n times in given list
    Given a list, the task is to find whether any element occurs 'n' times in given list of integers. It will basically check for the first element that occurs n number of times. Examples: Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3 Output : 2 Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4
    5 min read
  • Get first and last elements of a list in Python
    The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way
    3 min read
  • Python program to find sum of elements in list
    Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently. Using sum()sum() function is t
    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