list number of items in list python code example

Example 1: python number of elements in list of lists

# Basic syntax:
# Using list comprehension:
sum([len(elem) for elem in list_of_lists])

# Example usage:
# Say you want to count the number of elements in a nested list like:
nested_list = [ [1, 2, 3, 45, 6, 7],
                [22, 33, 44, 55],
                [11, 13, 14, 15] ]

sum([len(elem) for elem in nested_list])
--> 14

Example 2: number of elements list python

>>> mylist = [1,2,3] #list
>>> len(mylist)
3
>>> word = 'hello' # string 
>>> len(word)
5
>>> vals = {'a':1,'b':2} #dictionary
>>> len(vals)
2
>>> tup = (4,5,6) # tuple 
>>> len(tup)
3