Skip to content
Home » How to Find Length of List in Python

How to Find Length of List in Python

In this tutorial we will learn about How to Find Length of List in Python. In Python, a list is an ordered and modifiable collection datatype. Lists enable the storage of several elements in a single variable. A list can also contain duplicate elements. We will learn how to find the length of a list in Python using several ways in this lesson.

List in Python

In Python, a list is a collection of items/values that can hold one or more data types such as string, integer, float, and so on. Python has six more data types. Lists, on the other hand, are the most often used data type in Python. Lists are created using square brackets [], and the elements in list are separated by commas ( , ).

Example:

# List of integers
integers = [1, 2, 3]

# List of strings
strings = ["similar", "geeks", "python"]

# List of different data types
sample = [1.24, "python", 3]

Different ways to find the Length of the List Python

In Python, there are two ways to find the length of a list: you may use the built-in len() method, or you can utilize iteration, i.e., the for loop or while loop. I hope you now understand how to calculate the length of a list in Python.

How to Find the Length of the List in Python ?

There are two methods for determining Python list length.

  1. By using built-in len() method
  2. By Iterating the elements in the List using Loop

Using built-in len() method

Python list method len() returns the number of elements in the list.

Syntax

Syntax for len() method is :

len(list)

Parameters

  • list − A list for which number of elements has to be counted.

Example:

In Python, the len() function is the most commonly used and convenient technique for determining the length of a list. The example below will output the length of a list.

sample = [1.24, "python", 3]
print("List : ",sample)
print("Length of the list = ", len(sample))

Output:

List : [1.24, “python”, 3]
Length of the list = 3

By Iterating the elements in the List using Loop

Iteration is a fairly easy method in which you iterate the list using a for loop by setting a counter and incrementing the counter until you reach the last element.

If you don’t want to learn how to utilize the built-in Python list len() method, you could possibly use the iteration strategy (for-loop or while-loop). Using this strategy, you could even create your own way.

Example:

sample = [1.24, "python", 3]
print("List : ",sample)

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

Output:

List : [1.24, “python”, 3]
Length of the list = 3

Similar Posts:

Tkinter | Python Script for Distance Converter from KM to Yard, Mile and Foot  using Tkinter
Python | Convert Celsius to Kelvin Temperature using Tkinter