count() method in Python List

It is a method in list that counts the number of times a particular element occurs in the given list.

Syntax: list.count(element)

Don’t know what is append method in python? Read: append() method in Python List

Example 1:

s=["Name", "codewindow", "Country", "India", "codewindow"]
x=s.count("codewindow")
print(x)

Output:

2

Explanation:

Here the element “codewindow” occurs 2 times in the list. Hence returned 2.

Example 2:

s=["Name", ["codewindow", "Country"], "India", ["codewindow", "Country"], "state"]
x=s.count(["codewindow", "Country"])
print(x)

Output:

2

Explanation:

Here the list “[“codewindow”, “Country”]” occurs 2 times in the parent list. Hence returned 2.


You Missed

Also Checkout