A 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 the len() method.
len()
is a function used to check the number of items in a list. It returns 0 if the list is 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)