Find size of a list in Python

List in Python is a collection of primitive data type or object which is ordered and can be added or removed. We check the length of a list with len() method.

len() is a function which used to check the number of items in a list. It returns 0 if the list if empty.

my_list = []
len(my_list) #output: 0
odd_number_list = [1, 3, 5, 7]
len(odd_number_list) #output: 4

We often check the length of a list for iterating through it.

phones = ['Samsung', 'LG', 'Appel', 'Xiao Mi']
if len(phones) > 0:
    for phone in phones:
         print(phone) 

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close