AppDividend
Latest Code Tutorials

Python List length: How to Find a Length of List

0

A list in Python is used to store the sequence of various types of data. A list is defined as a collection of values or elements of different types. The elements in the list are separated with a comma(,) and surrounded with the square brackets [ ].

Python List length

To find the length of the list in Python, use the len() method. The len() is a built-in Python method that takes a total number of elements in the list and returns the length of a list.

The len() method can be used with a list, tuple, arrays, dictionary, etc. The len() method is one of the most used and convenient ways to find the length of a list in Python.

Syntax

len(list)

Argument

The len() function takes a list parameter in which the number of elements is counted. It returns the number of elements in the list.

Example

listA = ["Jeff", "Bezos", "Is", "Retiring", "from", "Amazon"]

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

Output

The length of the list is: 6

The len() method works in O(1) time as a list is an object and has a member to store its size.

Is the len() method specific to the list?

No, the len() method is not specific to the list. In the len() method, you may provide the object that can be a sequence (list, tuple, string, bytes, or range) or collections such as a dictionary.

Using the len() function, you can get the length of the total number of elements in a set or frozenset.

How to Get Length of an Array in Python

To find a length of an array in Python, use the len() method. To work with an array in Python, import the array module and then create an array using the array() method with some elements, and then we will find the length of that array.

from array import array 

arr_len = array('b',[11, 19, 21, 18, 46])
print("Number of elements in an Array:", len(arr_len))

Output

Number of elements in an Array: 5

That is it for this tutorial.

Leave A Reply

Your email address will not be published.

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