python

Get the length of a list in Python

If you are working on lists in Python and want to get the length of the list, you can use the len() function that returns the length of the list.

# define a list
my_list = ['a', 'b', 'c', 'd']

# Get length of the list using len() method
result = len(my_list)

# Print the result
print(result)
Output
4

The len() function takes the list as a parameter and returns its length in integer format. In the above example, we are passing a list my_list inside the len() function. The list has four items in it so the function len(my_list) will return 4.

Syntax

len(List)
Was this helpful?