How to Get the Length of 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.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.