How to get the length of a list in python ?


Examples of how to get the size of a list in python

Get the length of a list

To get the size of a list in python, a solution is to use the python function len, examples:

>>> a = ['bob','ben','Tim']
>>> len(a)
3

another example with an empty list:

>>> l = []
>>> len(l)
0

Another solution is to iterate in the list using a loop:

>>> a = ['bob','ben','Tim']
>>> list_size = 0
>>> for i in a:
...     list_size = list_size + 1
... 
>>> list_size
3

Check if a list is empty

Example of how to check if a list is empty

>>> def list_is_empty(list):
...     if len(list) == 0:
...             print(True)
...     else:
...             print(False)
... 
>>> list = [1,2,3]
>>> list_is_empty(list)
False
>>> 
>>> list = []
>>> list_is_empty(list)
True
>>>