How To Find Size of List in Python?

The list is a popular type in the Python programming language. List contains items that can be different types like string, integer, floating-point, list, dictionary, etc. The size of the list is equal to the count of the list items.

len() Method

Python provides the len() method where the list can be provided as parameter the the size or item count of the list will be returned as integer. The len() method is a built-in method which is provided by default and there is no need to import any extra module.

mylist = [ 1 , 2 , 3 , 4 , 5 ]

print("Size of the list is",len(mylist))

# Output
# Size of the list is 5



mylist = [ "a" , "b" , "c" , 1 ,2 , 3 ]

print("Size of the list is",len(mylist))

# Output
# Size of the list is 6



mylist = [ "a" , "b" , "c" , [ 1 ,2 , 3 ] ]

print("Size of the list is",len(mylist))

# Output
# Size of the list is 4

Get Length Of A Line In A List

Well, you have learned how to find a list length. But how can you find the length of a list in a list? You can provide the list of lists to the len() method that will return the element count. In the following example, we will provide the mylist 3rd element to the len() method which is also a list. The output will be 3 .

mylist = [ "a" , "b" , "c" , [ 1 ,2 , 3 ] ]

print("Size of the list is",len(mylist[3]))

# Output
# Size of the list is 3

Leave a Comment