Skip to content

How to find the length of a list in Python?

Python is one of the most popular and easy to learn programming languages around at the moment. While the syntax may be relatively easier to understand, the language is still quite powerful and has most if not all the tools a relatively higher-level programming language might.

Today we’re talking about lists in Python and how you can find the length of a list.

What is a Python list?

A list in Python or any other programming language is a data type used to store a sequence of various other data types. It’s exactly what it sounds like — it’s a list. 

There are other methods to store sequences; in fact, there are six, but lists are one of the most popular ones. It’s defined as a collection of values or items of different types separated by commas and enclosed within square brackets. 

Here’s the syntax used to define Python lists.

listname = ['item1', 2, 'item3']

Also read: Python vs Java vs C/C++: Key differences and Pros-Cons

How to find a Python list length?

There are two methods you can use to find a list’s length. 

  • Using the len() function.
  • Using a loop.

We’ll be going over both methods.

Finding Python list length using the len() function

The len() function is a built-in method for finding the total number of items in sequenced data types such as lists, tuples, arrays, and dictionaries. All the function needs is an argument providing the list in question. 

Here’s the basic syntax used with the function.


Here’s an example.

ListName = ["Hello", "World"]
print ("Length of the list = ", len(ListName))

The output for the above snippet will be 2, as there are two items in the list.

Finding Python list length using a loop

Alternatively, you can use a loop to go over each list item and operate on them. 

Here’s an example.

list= ['This', 'is', 'a', 'list']
counter = 0

for capital in capitals:
counter = counter + 1

print("The list has {0} elements.".format(counter))

We recommend using the len() method every time you need only to find the length of a list as the function is a lot less complicated to write and is purpose-built for that functionality. If you need to operate on a list’s items, however, you will have to use the looping method.

Also read: Top 7 Python IDEs and Text Editors for Data Science Applications

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

Share on facebook
Share on whatsapp
Share on twitter
Share on reddit
Share on linkedin
Share on pocket
Share on pinterest
Share on telegram
Share on stumbleupon
Share on digg
Share on tumblr
Share on email
Share on skype
Share on xing
Share on vk
Share on odnoklassniki
Share on mix