Computer >> Computer tutorials >  >> Programming >> Python

How do we get the size of a list in Python?


To find the size of a list, use the builtin function, len. The python docs state:

"len(arg) returns the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set)."

len is a builtin and can also be used on user defined classes that implement __len__. So this operation may be O(n) or O(1) depending on the implementation of __len__.

example

list1 = [1, 2, "Hello"]
print(len(list1))
my_str = "Hello"
print(my_str)

Output

This will give the output −

3
5