Find size of a list in Python


A list is a collection data type in Python. The elements in a list are change able and there is no specific order associated with the elements. In this article we will see how to find the length of a list in Python. Which means we have to get the count of number of elements present in the list irrespective of whether they are duplicate or not.

Examples

In the below example we take a list named as "days". We first find the length of the list using len() function. And then we add few more elements and check the length again using append() function. Finally we remove some elements using remove() function and check the length again. Please note even though the elements are duplicate the remove() function will remove only e the elements at the end of the list

days = ["Mon","Tue","Wed"]
print (len(days))
# Append some list elements
days.append("Sun")
days.append("Mon")
print("Now the list is: ",days)
print("Length after adding more elements: ",len(days))

# Remove some elements
days.remove("Mon")
print("Now the list is: ",days)
print("Length after removing elements: ",len(days))

Output

Running the above code gives us the following result −

3
Now the list is: ['Mon', 'Tue', 'Wed', 'Sun', 'Mon']
Length after adding more elements: 5
Now the list is: ['Tue', 'Wed', 'Sun', 'Mon']
Length after removing elements: 4
raja
Published on 07-Aug-2019 12:16:48
Advertisements