Best way of using Python List Length

  • Reading time:5 mins read

What is Python list length, and what is the Python len() function used for?

The Python len() method is a built-in function that is used to calculate the length of any iterable objects such as lists, tuples.

 

How to use the Python list length function?

When the object is a list the len() method can be used to determine the length of the list. This is particularly useful when working with large projects that involve large lists and you would want to know how many items are on the list. 

 

Python list length

Using the Python list length function on a string list

In this case, we’re going to create a list that is going to allow us to experiment with the len() function that is going to allow us to know exactly how many items are in the list.

 

 

Now to determine the length of the list above we simply need to pass in the name of the list within the parentheses and print out the length.

 

 

This is particularly useful if you have long lists or when working with lists nested within other lists.  This can also be useful when working with very large lists and you would want to make sure that the data you have in one place matches the other.

 

Python list length

 

An easier way of doing that is just checking the length of each list to make sure that they match. For instance, if we have a list inside our main list, we can see that the length of the list will still remain to be seven.

This is because the intention determines the number of items inside the main list.

 

Using the Python list length function on a list of tuples

If we change one of the elements of the list to a tuple, we are still going to get that the length remained seven. This is because we are still having seven objects inside the main list, and the tuple we’ve just created is considered one of them.

 

Using the Python list length function on nested lists

We can also determine the number of elements within a nested list by simply indexing the nested list whose length we want to determine.

 

 

In this case, we are determining the length of the last object in the main list which is another list that is nested within the main list. So we have three items on this list.

 

Python list length

 

Suppose we add another element within that list for example if we add ‘bear’ and again determine the new length of the list, we’ll get a different length this time.

 

 

So that’s a really easy quick way to know how many items are within your list. It is also good to note that actual items in a list won’t necessarily just be strings but can also be other types of iterable objects such as tuples or even lists themselves.

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

 

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.