| by Arround The Web | No comments

Python List Length

Lists” are one of the most versatile and commonly utilized Python data structures. Lists are capable of storing almost any type of object, such as numbers, dictionaries, strings, or even other lists. When traversing or performing operations on a particular list, we have to calculate/determine its length. To compute the length of the list in Python, various functions such as the “len()” function, or the “length_hint()” method are utilized in Python.

This post elaborates on determining the list length in Python.

How to Determine the Python List Length?

To determine the list length or the number of elements/items in the particular list, the below methods are utilized in Python:

Method 1: Determine the Python List Length Using the “len()” Function

The “len()” function is utilized in Python to retrieve the list length by accepting the particular list as an argument.

Example

This example code is used to determine the list length:

list_1 = [35, 56, 47, 12]
print('Given List: ', list_1)
print('\nList Length: ', len(list_1))

Based on the above code, the “len()” function accepts the initialized list as an argument and retrieves its length.

Output

This snippet verified that the list length has been determined.

Method 2: Determine the Python List Length Using the “for” Loop

The “for” loop can also be utilized to determine the list length.

Example

The following code gets/determines the list length using the “for” loop:

list_1 = [13, 76, 67]
print('Given List: ', list_1)
value = 0
for x in list_1:
    value += 1
print('\nList Length: ',value)

According to the above code:

  • The “list” and the variable containing the value “0” are initialized.
  • The “for” loop iterates through the list and adds the int value “1” to a variable called “value” for each element found in the list.
  • In this way, the “for” loop computes the corresponding list length.

Output

The list length has been returned appropriately utilizing the “for” loop.

Method 3: Determine the Python List Length Using the “length_hint()” Method

In Python, the “length_hint()” method of the “operator” module is utilized to estimate the input iterable object length. It computes the actual length if the length is known and retrieves an estimated length if it is unknown.

Example

Let’s overview the below example code:

from operator import length_hint
list_1 = ['Mary', 'Cynda', 'Kelly', 'Gray']
print('Given List: ', list_1)
print('\nList Length: ',length_hint(list_1))

In the above block of code, the “length_hint()” method of the “operator” module is utilized to determine the length of the defined strings list by accepting it (list) as an argument.

Output

It is verified from the above outcome that the length of the strings list has been determined.

Conclusion

The “len()” function, the “for” loop, or the “length_hint()” method is utilized in Python to determine/get the length of the list. The “len()” and “length_hint()” functions are simple and straightforward to compute the list length. A list can be passed into these functions and retrieve the corresponding length. The “for” loop, however, returns the list length via iteration. This tutorial presented multiple ways to determine the list length in Python using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply