Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Find Length of List in Python Like a Pro

In this tutorial, we will learn how to find the Length of a list in Python with the help of examples.

Python is a very popular and powerful programming language that we use for a variety of tasks. One of the most common tasks when working with the Python list is to find the length of a list.

Lists are one of the most fundamental data types in Python that we used to store collections of elements or values, such as numbers, strings, and other objects.

Finding the length of a list is a fundamental operation in Python. It is quite simple to find once you know the right syntax and method.

In this tutorial, we will explore four different ways to get the length of a list.  So, let’s understand it step by step.

Finding Length of List in Python


Before going to find the length of a list in Python, let us understand some basics of lists in Python.

A list is an order sequence or collection of elements or data, which can be of different data types, such as strings, integers, floats, objects, etc.

We define lists using square brackets ([ ]), enclose elements in square brackets separated by commas, and assign it a user-defined variable.

Here’s a simple example of a list in Python:

fruit_list = ["Mango", "Orange", "Banana", "Guava"]

In this example, fruit_list is a list of four items or elements, all of which are string types. Here, we have created a list using square brackets ([ ]) and enclosed elements in square brackets separated by commas.

Now let’s move on to the main topic.

The length or size of a list represents the number of elements present in a list. There are several ways by which we can find the length of a list in Python. They are:

  • len() method
  • for loop (known as native method)
  • length_hint() function
  • enumerate() function

Method 1: Using len() Function


Python provides a very simple built-in len() function for finding the length of a list. It is easiest and most straightforward way to find size of a list.

The len() function takes a list of elements as an argument and returns the number of elements present in the list. The general syntax to define a len() function is as:

len(s)

Here’s a simple example of how to use the len() function to find the length of a list.

Example 1:

# Create a list of four elements of string type.
fruit_list = ["Mango", "Orange", "Banana", "Guava"]
print("Length of list =",len(fruit_list))
Output:
      Length of list = 4

Example 2:

# Create a list of five numbers.
num_list = [20, 40, 60, 80, 100]

# Call len() function and pass the list variable.
length = len(num_list)
print("Length of list =",length)
Output:
      Length of list = 5

In this example, we have created a list of five integer numbers and assigned it to a variable num_list. Then, we have called len() function and passed the variable num_list to find the length of list and stored it into a variable named length. Finally, we have displayed the length of list on the console.

Method 2: Using for loop to find length of a list


The len() function is the most commonly used by programmers to find the length of the list in Python. However, programmers also use for loop to find the length of a list. This is another basic way to find list size.

In this method, for loop iterates over all list elements. In each iteration of the loop, we will increment a counter variable till loop has iterated over all the elements in the list.

Example 3:

# Python program to demonstrate length of list using for loop.
# Creating a list of five numbers.
num_list = [10, 40, 50, 70, 80]
# Printing num_list
print("The list is :",num_list)

# Finding length of list using for loop
# Initializing counter
counter = 0
for i in num_list:
    counter = counter + 1 # incrementing counter

# Displaying length of list
print("Length of list using for loop:",counter)
Output:
      The list is : [10, 40, 50, 70, 80]
      Length of list using for loop: 5

Method 3: Using length_hint() to get length of a list


Python operator module provides a function named length_hint() to get the length of a given iterable object like list, set, etc. Howvere, this method is not so popular for finding list length.

In this method, if the length is known, the length_hint() method returns the actual length of a list. Else, the length_hint() method returns an estimated length of a list. The general syntax of length_hint() is as:

length_hint(object)

Here’s a simple example program in which we have found the list length using len() and length_hint() functions.

Example 4:

# Python program to demonstrate length of list using len() and length_hint.
from operator import length_hint
# Creating a list of five numbers.
mixed_list = ["Python", 1, 2, 3, ['Technology', 20, 20.5]]

# Printing mixed_list.
print("List:",mixed_list)

# Finding length of list using len() function.
list_len = len(mixed_list)

# Finding length of list using length_hint().
list_len_hint = length_hint(mixed_list)

# Displaying the length of list.
print("Length of list using len():",list_len)
print("Length of list using length_hint():",list_len_hint)
Output:
      List: ['Python', 1, 2, 3, ['Technology', 20, 20.5]]
      Length of list using len(): 5
      Length of list using length_hint(): 5

Method 4: Using enumerate() Function


A fourth way to get the length of a list in Python is to use the enumerate() function. We use the enumerate() function to loop over elements of the list and return both the index and the item at each iteration.

Here is a very simple example of how to use the enumerate() function to find the list length.

Example 5:

# Python code to get length of list using enumerate().
# Creating a list.
name_list = ["John", "Bob", "Mark", "Deep"]
print("List:",name_list)

count = 0
for index in enumerate(name_list):
    count += 1
# Printing length of list
print ("Length of list using enumerate():",count)
Output:
      List: ['John', 'Bob', 'Mark', 'Deep']
      Length of list using enumerate(): 4

In this example, we have created a list of four string elements and assigned it to a variable named name_list. Then, we have used the enumerate() function to loop over its elements.

We have also initialized a variable named count with 0, which we will use to count the number of iterations or loops. The enumerate() is a built-in function that returns the index (position) and item at each iteration, but we are only interested in the index.

Therefore, we have neglected the item variable. Finally, we have displayed the value of count, which gives us the length of list named name_list.


In this tutorial, you have learned about how to find the length of a list in Python with examples. You learned four ways to get the length of a list.

Out of four, the len() function is the most common and efficient way to find the list length. However, you can also use other approaches, such as for loops, and length_hint().

All four ways are simple and easy to use. Therefore, choose the one that works best for you and start working with lists in Python like a pro! Hope that you will have understood all the basic points in regarding with getting the length of a list.

Thanks for reading!!!

The post How to Find Length of List in Python Like a Pro appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

How to Find Length of List in Python Like a Pro

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×