Get size of list python

In Python, you use a list to store various types of data such as strings and numbers.

A list is identifiable by the square brackets that surround it, and individual values are separated by a comma.

To get the length of a list in Python, you can use the built-in len() function.

Apart from the len() function, you can also use a for loop and the length_hint() function to get the length of a list.

In this article, I will show you how to get the length of a list in 3 different ways.

You can use the native for loop of Python to get the length of a list because just like a tuple and dictionary, a list is iterable.

This method is commonly called the naïve method.

The example below shows you how to use the naïve method to get the length of a list in Python

demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]

# Initializing counter variable
counter = 0

for item in demoList:
    # Incrementing counter variable to get each item in the list
    counter = counter + 1

    # Printing the result to the console by converting counter to string in order to get the number
print("The length of the list using the naive method is: " + str(counter))
# Output: The length of the list using the naive method is: 7

How to Get the Length of a List with the len() Function

Using the len() function is the most common way to get the length of an iterable.

This is more straightforward than using a for loop.

The syntax for using the len() method is len(listName).

The code snippet below shows how to use the len() function to get the length of a list:

demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]

sizeOfDemoList = len(demoList)

print("The length of the list using the len() method is: " + str(sizeOfDemoList))
# Output: The length of the list using the len() method is: 7 

How to Get the Length of a List with the length_hint() Function

The length_hint() method is a less known way of getting the length of a list and other iterables.

length_hint() is defined in the operator module, so you need to import it from there before you can use it.

The syntax for using the len() method is length_hint(listName).

The example below shows you how to use the length_hint() method to get the length of a list from operator import length_hint:

demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22]

sizeOfDemoList = length_hint(demoList)
print("The length of the list using the length_hint() method is: " + str(sizeOfDemoList))
# The length of the list using the length_hint() method is: 7

Final Thoughts

This article showed you how to get the size of a list with 3 different methods: a for loop, the len() function, and the length_hint() function from the operator module.

You might be wondering which to use between these 3 methods.

I would advise that you use len() because you don't need to do much to use it compared to for loop and length_hint().

In addition, len() seems to be faster than both the for loop and length_hint().

If you find this article helpful, share it so it can reach others who need it.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

The list in Python is a collection data-type that is ordered and changeable. A list can have duplicate entry as well. The Python len() method is used to find the length of any object. In this article, we will learn how to find the length of list in python programming in the following sequence:

  • List in Python
  • How to find the Length of List in Python?
    • Len() Method
    • Naive Method

List in Python

A list in Python is implemented to store the sequence of various types of data. However, there are six data types in Python that are capable of storing the sequences but the most common and reliable type is a list. To learn more about python you can join our Master Python programming course.

Get size of list python

A list is defined as a collection of values or items of different types. The items in the list are separated with a comma (,) and enclosed with the square brackets [].

It is defined as follows:

list1 = ['edureka', 'python', 2019];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

If you want to learn Artificial Intelligence and Machine Learning in-depth, come to us and sign up for this Post Graduate Diploma AI and ML courses.

How to Find the Length of List in Python?

There are two most commonly used and basic methods that are used to find the length of the list in Python:

  • Len() Method
  • Naive Method

Len() Method

There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.

The len() method is one of the most used and convenient ways to find the length of list in Python. This is the most conventional technique adopted by all the programmers today.

Find out our Python Training in Top Cities/Countries

India USA Other Cities/Countries
Bangalore New York UK
Hyderabad Chicago London
Delhi Atlanta Canada
Chennai Houston Toronto
Mumbai Los Angeles Australia
Pune Boston UAE
Kolkata Miami Dubai
Ahmedabad San Francisco Philippines

Syntax:

len(list)

The List parameter is a list for which the number of elements are to be counted. It returns the number of elements in the list.

Example:

ListName = ["Hello", "Edureka", 1, 2, 3]
print ("Number of items in the list = ", len(ListName))

Output:5

Naive Method

The len() method is the most commonly used method to find the length of the list in Python. But there is another basic method that provides the length of the list.

In the Naive method, one just runs a loop and increases the counter until the last element of the list to know its count. This is the most basic strategy that can be used in the absence of other efficient techniques.

Example:

ListName = [ "Hello", "Edureka", 1,2,3 ]
print ("The list is : " + str(ListName))
counter = 0
for i in ListName:
counter = counter + 1
print ("Length of list using naive method is : " + str(counter))

Output:

The list is : ["Hello", "Edureka", 1,2,3]
Length of list using naive method is : 5

This was all about finding the length of list in Python. The len() method is the most popular method. Whereas, you can also use the basic method of finding the length with the help of Naive Method.

With this, we have come to the end of our article. I hope you understood how to find the length of any list in Python.

To get in-depth knowledge of Python along with its various applications, you can enroll for live Python Certification Training with 24/7 support and lifetime access. 

Got a question for us? Please mention it in the comments section of this “Length of List in Python” blog and we will get back to you as soon as possible or join our Python Training in Chennai.

Upcoming Batches For Python Certification Training Course

Course NameDate
Python Certification Training Course

Class Starts on 24th September,2022

24th September

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 29th October,2022

29th October

SAT&SUN (Weekend Batch)
View Details

How do you find the length of a list?

Len() Method There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.

How do you find the length of a list without using Len in Python?

How to implement without using len():.
Take input and pass the string/list into a function which return its length..
Initialize a count variable to 0, this count variable count character in the string..
Run a loop till length if the string and increment count by 1..

How do you count the number of elements in a list in Python?

Using Len() function to Get the Number of Elements We can use the len( ) function to return the number of elements present in the list.

How do you find the length of a list in Python w3schools?

Python len() Function The len() function returns the number of items in an object.