DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

How to Find the Length of List in Python?

ItsMyCode |

The list in Python is a collection datatype that is ordered and mutable. Lists allow you to store multiple elements in a single variable. A list can have duplicate elements as well. In this tutorial, we will learn how to find the length of a list in Python using different approaches.

List in Python

A list in Python is a collection of items/values that can store one or more data types such as string, integer, float, etc. There are six other data types in Python. However, lists are the most commonly used data type in Python.

Lists are created using square brackets [], and the elements in the list are separated using commas.

The list is defined as follows –

# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]

Enter fullscreen mode Exit fullscreen mode

length of list python

How to Find the Length of List in Python?

There are two approaches in Python to find length of list.

  1. Using the built-in len() method
  2. Iterating the elements in List

Using the built-in len() method

Python has a built-in function to find the total number of items in a list, arrays, tuple, dictionary etc.

The len() method accepts the list as an argument and returns the length of the list in Python.

Syntaxlen(list)

Example

The len()method is the most widely used and convenient way to get the length of the list in Python. The below example will print the length of a list.

# List with integers
numbers = [1, 2, 3, 4, 5]
# List with string
fruits = ["apple", "orange", "grapes"]
# List with mixed data type
mixedlist = [1, "Ninja", 55.22, False,"a"]

print("Length of numbers = ", len(numbers))
print("Length of fruits = ", len(fruits))
print("Length of mixed list = ", len(mixedlist))

# Output
Length of numbers = 5
Length of fruits = 3
Length of mixed list = 5
Enter fullscreen mode Exit fullscreen mode

Iterating the elements in List

The iteration method is a very simple way where you iterate the list using a for loop by setting a counter and incrementing the counter till you reach the last element.

If you do not want to use the built-in method and learn how it works behind the scenes, then you could probably use the iteration approach. You could also build your own method by using this approach.

mixedlist = [1, "Ninja", 55.22, False,"a"]

print("The current list is : ",mixedlist)

count=0
for i in mixedlist:
    count=count+1
print("Length of mixed list = ", count)

# Output
The current list is : [1, 'Ninja', 55.22, False, 'a']
Length of mixed list = 5
Enter fullscreen mode Exit fullscreen mode

Conclusion

In order to find the length of the list in Python, there are two approaches either you could use the built-in len() method, or you can use the iteration way, i.e., using for loop. I hope you have understood how to find the length of any list in Python.

The post How to Find the Length of List in Python? appeared first on ItsMyCode.

Top comments (0)