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

PythonServer Side ProgrammingProgramming

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
raja
Published on 05-Jan-2018 09:22:52
Advertisements