This article shows how to count list elements.
Contents
- How to count elements in list ( Get list size ).
- How to count elements in two-dimensional list.
- How to count the number of specific elements in list
sponsored link
How to count elements in list ( Get list size ).
I show how to count elements in list.
The "len() function" can be used to get the number of elements in list.
The "len() function" is built-in function in Python.
How to write
len( List )
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(len(list_1))
# 5
sponsored link
How to count elements in two-dimensional list.
I show how to count elements in two-dimensional list.
Use the "len() function" even for two-dimensional lists.
If you want to know the number of rows, specify a list as an argument.
If you want to know the number of columns, specify a element of list as an argument.
Here is an example code.
list_2 = [
[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
]
print(len(list_2))
# 3
print(len(list_2[0]))
# 4
sponsored link
How to count the number of specific elements in list
I show how to count the number of specific elements in list.
The "count" method can be used to count the number of specific elements contained in a list.
How to write
List.count( Value )
Here is an example code.
list_1 = ["Apple", "Banana", "Orange", "Bana", "Banana"]
print(list_1.count("Apple"))
# 1
print(list_1.count("Ban"))
# 0
By the way, the following article describes how to search for elements in the list.
How to Find Element in List [exact match, partial match]
sponsored link