How to find the length of a list in Python

In this article, you’ll learn how to find the length of a list in Python. I highly recommend you get the “Python Crash Course Book” to learn Python. To find the length of a list you can use these two methods:

  • Method 1: len() method
  • Method 2: For Loop

Method 1: length of a list in Python using len() method

This built-in method is mostly used to get the length of an object in Python. In the following example, the len() method is used to find the length of the list.

# Create a list
a = [25, 10, 5.2, 43, 'AI']

# Display the length of a list
print(len(a))

Output:

5

Method 2: For Loop

You can also find the length of a list using for loop.

# Create a list
a = [25, 10, 5.2, 43, 'AI']

# For Loop to count the items of a list
length = 0
for i in a:
    length = length + 1
    
# Display the length of a list
print(length)

Output:

5

Leave a Comment

Your email address will not be published.