How to Get the Length of a Python List?

Find out how to get the total number of elements in a python list

To get the length/size of a list in Python, we can simply use the built-in len() function like so:

list = []
list.append('foo')
list.append('bar')
list.append('baz')

print(len(list)) # output: 3

The len() function takes an object as an argument and returns the total number of items (i.e. the length of the object). It has the following syntax:

len(object)

Since a list in Python is an object, the len() function works for it and returns the total number of items in the list.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.