Home » Python » How to find the length of a list in Python

About Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).

How to find the length of a list in Python

Hello in this tutorial, we will see how to determine the length of a list in python programming.

1. Introduction

The list data structure in python programming is:

  • A data structure that can store multiple data at once (in other words it is a comma separate items between the square brackets and the items can be of the different or same type)
  • It is an ordered collection
  • Each element in a list have a distinct place in the sequence and will not eradicate the duplicates
  • It is mutable in nature (i.e. we can add and remove the entries

python list length

There are two common ways to determine the length of a list in python programming i.e.

  • Using the in-built len() method – This method takes an argument such as a list, tuple, or an array and returns the count of the elements
    • Represented by the syntax as – len(argument)
    • Recommended approach as it has memory space available to store the size
  • Using the for loop to count the elements of a list and print the count

1.2 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. How to find the length of a list in Python

Let us dive in with the programming stuff now. I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Creating an implementation file

Let us understand the implementation with the help of a python script.

Implementation file

# find length of a list

# using len() method
def len_method(languages):
    size = len(languages)
    print('Number of elements in list = {}'.format(size))


# using native method
def loop_method(languages):
    total_items = 0
    for language in languages:
        # print(language)
        total_items += 1

    print('Number of elements in list using native method = {}'.format(total_items))


# driver code
if __name__ == '__main__':
    items = ['English', 'German', 'French', 'Russian', 'Spanish', 'Hindi']
    len_method(items)
    print('\n')
    loop_method(items)

print('\nDone')

If everything goes well the following output will be shown in the IDE console.

Console Output

Number of elements in list = 6


Number of elements in list using native method = 6

Done

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • Different ways to calculate the length of a list in Python
  • Sample program to understand the different techniques

You can download the source code of this tutorial from the Downloads section.

Learn more about Python in our tutorial for beginners.

4. Download the Project

This was a tutorial on determining the length of a list in python programming.

Download
You can download the full source code of this example here: How to find the length of a list in Python

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments