number of elements in a list python code example

Example 1: python list length

# example of len() function

list_number = [6, 3, 8, 0]

length = len(list_number)
print("The lentgh of the list is: " + str(length))

Example 2: python find the number of elements in a list

list1 = [2,3,4,3,10,3,5,6,3]
elm_count = list1.count(3)
print('The count of element: 3 is ', elm_count)

Example 3: 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 4: python how to count all elements in a list

List = ["Elephant", "Snake", "Penguin"]

print(len(List))

#	With the 'len' function Python counts the amount of elements 
#	in a list (The length of the list).
#	In this case the output is '3' because there are 3 elements in the list.

Example 5: how to find the amount of numbers in a list on python

>>> someList=[]
>>> print len(someList)
0

Example 6: 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

Tags:

Html Example