Python List count() Method

Python 列表Python Lists


Description

The count() method is used to count the number of times an element appears in a list.

Syntax

The syntax of the count() method is:

list.count(obj)

Parameters

  • obj -- The object to be counted in the list.

Return Value

Returns the number of times the element appears in the list.

Examples

The following example demonstrates the usage of the count() function:

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc', 123];

print "Count for 123 : ", aList.count(123);
print "Count for zara : ", aList.count('zara');

The output of the above example is as follows:

Count for 123 :  2
Count for zara :  1

Python 列表Python Lists

Other Extensions