How to Use List count() Method in PythonPython Basics


The count() method counts how many times an element occurred in a list.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

list1 = [123,'MIT', 'CIT', 'Yale','UCI','UCI','UCI',123]
print("the list1 is: ", list1)

print("the number of 123 is :",list1.count(123))
print("the number of UCI is: ",list1.count('UCI'))

Output:

the list1 is:  [123, 'MIT', 'CIT', 'Yale', 'UCI', 'UCI', 'UCI', 123]
the number of 123 is: 2
the number of UCI is:  3

Syntax

list.count(obj)

Parameters

Name Description
obj Obj whose count is to be found.

Return Value

It returns the number of occurrences an element appears in a list.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us