Search code snippets, questions, articles...

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)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
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?
0 Comments