Home » Python

Find the number of elements in a list in Python

Find the number of elements in a list: Here, we are going to learn how to find the total number of elements in a list in Python programming language?
Submitted by IncludeHelp, on March 05, 2020

To find the total number of elements in a List in Python – we use the len() method, it is an inbuilt method, it accepts an argument (this list) and returns the total number of elements.

Syntax:

    len(list_object/list_elements)

Note: As an argument, a list object or a direct list ( [elemen1, element2, ...]) can be passed.

Example 1:

# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]

# printing list and its length
print("list1: ", list1)
print("list2: ", list2)
print("len(list1): ", len(list1))
print("len(list2): ", len(list2))

# passing a direct list elements to the function
print("Here, elements are: ", len([10,20,30,40,50]))

Output

list1:  [10, 20, 30, 40, 50]
list2:  ['Hello', 'IncludeHelp']
len(list1):  5
len(list2):  2
Here, elements are:  5

Example 2:

# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]

# finding the lenght of the lists
# before appending the elements
print("Before appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))


# appending elements
list1.append(60)
list1.append(70)
list2.append(".com")
list3.append(30)

# finding the lenght of the lists
# after appending the elements
print() # prints new line
print("After appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))

Output

Before appending...
list1:  [10, 20, 30, 40, 50]
list2:  ['Hello', 'IncludeHelp']
list3:  ['Hello', 10, 20, 'IncludeHelp']
Elements in list1:  5
Elements in list2:  2
Elements in list3:  4

After appending...
list1:  [10, 20, 30, 40, 50, 60, 70]
list2:  ['Hello', 'IncludeHelp', '.com']
list3:  ['Hello', 10, 20, 'IncludeHelp', 30]
Elements in list1:  7
Elements in list2:  3
Elements in list3:  5

Counting the occurrences of an element in the List

To count the occurrences of a given element – we use List.count() method, it accepts an argument whose occurrences to be found and returns its occurrences.

Example:

# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]

# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))

# printing the occurrences of an element
print("occurrences of 10 in list1: ", list1.count(10))
print("occurrences of 60 in list1: ", list1.count(60))
print("occurrences of 70 in list1: ", list1.count(70))

print("occurrences of \"Hello\" in list2: ", list2.count("Hello"))
print("occurrences of \"World\" in list2: ", list2.count("World"))

print("occurrences of \"IncludeHelp\" in list3: ", list3.count("IncludeHelp"))

Output

list1:  [10, 20, 30, 40, 50, 10, 60, 10]
list2:  ['Hello', 'IncludeHelp']
list3:  ['Hello', 10, 20, 'IncludeHelp']
Elements in list1:  8
Elements in list2:  2
Elements in list3:  4
occurrences of 10 in list1:  3
occurrences of 60 in list1:  1
occurrences of 70 in list1:  0
occurrences of "Hello" in list2:  1
occurrences of "World" in list2:  0
occurrences of "IncludeHelp" in list3:  1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.