Python List length: How to Find Length of List in Python?

The list in Python is a collection datatype that is ordered and mutable. Lists allow you to store multiple elements in a single variable. A list can have duplicate elements as well. In this tutorial, we will learn how to find the length of list in Python using different approaches.

List in Python

A list in Python is a collection of items/values that can store one or more data types such as string, integer, float, etc. There are six other data types in Python. However, lists are the most commonly used data type in Python.

Lists are created using square brackets [ ], and the elements in the list are separated using commas.

The list is defined as follows –

# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]
Length Of List Python
Python List length: How to Find Length of List in Python? 2

How to Find the Length of the List in Python?

There are two approaches to determine Python list length.

  1. Using the built-in len() method
  2. Iterating the elements in the List

Using the built-in len() method

Python has a built-in function to find the total number of items in the list, arrays, tuple, dictionary etc. 

The len() method accepts the list as an argument and returns the length of the list in Python.

Syntax len(list)

Example

The len() method is the most widely used and convenient way to get the length of the list in Python. The below example will print the length of a list.

# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]

print("Length of numbers = ", len(numbers))
print("Length of fruits = ", len(fruits))
print("Length of mixed list = ", len(mixedlist))

# Output
Length of numbers =  5
Length of fruits =  3
Length of mixed list =  5

Iterating the elements in List using a for loop

The iteration method is a very simple way where you iterate the list using a for loop by setting a counter and incrementing the counter till you reach the last element.

If you do not want to use the built-in Python list len() function and learn how it works behind the scene, then you could probably use the iteration approach (for-loop). You could also build your own method by using this approach.

mixedlist = [1, "Ninja", 55.22, False,"a"]

print("The current list is : ",mixedlist)

count=0
for i in mixedlist:
    count=count+1
print("Length of mixed list = ", count)

# Output
The current list is :  [1, 'Ninja', 55.22, False, 'a']
Length of mixed list =  5

Conclusion

In order to find the length of the list in Python, there are two approaches either you could use the built-in len() method, or you can use the iteration way, i.e., using for loop. I hope you have understood how to find the length of the list in Python.  

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like