How to Find the Length of List in Python [With Examples]

A data structure that is an ordered sequence of elements and is mutable is called a list in python. Each element in the list is called an item and each item in the list is identified by an integer value called index number starting from 0. The total number of items in the list is called the length of the list in Python

There Are Two Methods to Find the Length of the List in Python, They Are:

  • len() Method

Ø  The len() function is a built-in function in Python to determine the length of a list in Python

Ø  The len() function takes one parameter, namely list_name

Ø  The parameter list_name is the name of the list whose length is to be determined

Ø  The len() function takes the list_name or name of the list as the parameter and returns the length of the given list as the result

The syntax to define len() function in Python is as follows:

len(list_name)

where list_name is the name of the list whose length is to be determined.

Example 1:

Use the Python program to create a list, add elements to the list using the append() function, and then make use of the len() function in Python to determine the length of the list. And then display the length of list in Python as the output on the screen:

#creating an empty list called new_list

new_list = []

#adding elements to the list using append() function

new_list.append("Welcome")

new_list.append("to")

new_list.append("Simplilearn")

#determining the length of list in python using len() function

print("The length of the given list is: ", len(new_list))

The output of the above program is shown in the snapshot below:

lengthinPython_Exp_1 

In the above program, you are creating an empty list, adding elements to the list, and then determining the length of the list using the len() function in Python. The output is shown in the snapshot above.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Example 2:

Use the Python program to create a list, add elements to the list using the append() function, and then make use of the len() function in Python to determine the length of the list. And then display the length of list in Python as the output on the screen:

#creating an empty list called new_list

new_list = []

#adding elements to the list using append() function

new_list.append(10)

new_list.append(20)

new_list.append(30)

new_list.append(40)

new_list.append(50)

#determining the length of list in python using len() function

print("The length of the given list is: ", len(new_list))

The output of the above program is shown in the snapshot below: 

lengthinPython_Exp_2 

In the above program, an empty list is created, elements are added to the list and then the length of the list is determined using the len() function in Python. The output is shown in the snapshot above.

  • Naïve Method

Ø  The Naïve method in Python is one of the methods to determine the length of a list in Python

Ø  The Naïve method is writing a block of code to determine the length of a list in Python

Ø  There is a variable called counter which is incremented every time as we iterate through the given list using for loop

Ø  The Naïve method provides the length of the given list as the result

The syntax to define Naïve method in Python is as follows: 

counter = 0

for i in list_name:

counter = counter + 1

Where list_name is the name of the list whose length is to be determined, and the counter is the variable to keep track of the length of the list.

list_name represents the name of the list whose length is to be determined and ‘i’ is the variable used in the for loop to iterate through the loop.

Example 3:

Use the Python program to create a list, add elements to the list using the append() function, and then make use of the Naïve method in Python to determine the length of the list. And then display the length of list in Python as the output on the screen:

#creating an empty list called new_list

new_list = []

# adding elements to the list using append() function

new_list.append("Welcome")

new_list.append("to")

new_list.append("Simplilearn")

# determining the length of the list in python using Naive method in which we iterate through the given list using for loop and counter variable

counter = 0

for i in new_list:

counter = counter + 1

#displaying the length of the list as the output on the screen

print("The length of the given list is: ", counter)

The output of the above program is shown in the snapshot below:

lengthinPython_Exp_3 

In the above program, an empty list is created, elements are added to the list and then the length of the list is determined using the naïve method. The output is shown in the snapshot above.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Example 4:

Use the Python program to create a list, add elements to the list using the append() function, and then make use of the Naïve method in Python to determine the length of the list. And then display the length of list in Python as the output on the screen:

#creating an empty list called new_list

new_list = []

# adding elements to the list using append() function

new_list.append(10)

new_list.append(20)

new_list.append(30)

new_list.append(40)

new_list.append(50)

# determining the length of the list in python using Naive method in which we iterate through the given list using for loop and counter variable

counter = 0

for i in new_list:

counter = counter + 1

#displaying the length of the list as the output on the screen

print("The length of the given list is: ", counter)

The output of the above program is shown in the snapshot below:

lengthinPython_Exp_4 

In the above program, an empty list is created, elements are added to the list and then the length of the list is determined using the naïve method. The output is shown in the snapshot above.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion

With this tutorial, you’ve understood the concept of determining the length of a list in Python using the len() function in Python and the Naïve method in Python with corresponding programming examples and their outputs to demonstrate them. Simplilearn offers Python Certification Course designed to help you learn everything in Python to kick start your career in Python and this is a very flexible way to acquire skills in Python.

Do you have any questions regarding this tutorial? Leave them in the comments section, and our experts will get back to you on the same, right away!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.