TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python List count() method with Examples

Python List count() method with Examples on append(), clear(), extend(), insert(), pop(), remove(), index(), count(), pop(), reverse(), sort(), copy(), all(), bool(), enumerate(), iter(), map(), min(), max(), sum() etc.

<< Back to PYTHON

Python List count() Method

Python count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0. The examples and syntax is described below.

Signature

count(x)

Parameters

x: element to be counted.

Return

It returns number of times x occurred in the list.

Let's see some examples of count() method to understand it's functionality.

Python List count() Method Example 1

It is a simple example to understand how a count method works.

# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('p')
# Displaying result
print("count of p :",count)

Output:

count of p : 2

Python List count() Method Example 2

If the passed value is not present in the list, the method returns 0. See the example below.

# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('b')
# Displaying result
print("count of b :",count)

Output:

count of b : 0

Python List count() Method Example 3

Python count() method can also be used to check whether an element is duplicate in the list or not. The below example checks the duplicacy of the element in the list.

# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('p')
# Displaying result
if count>=2:
    print("value is duplicate")
print("count of p :",count)

Output:

value is duplicate
count of p : 2

Next TopicPython Lists




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf