Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python count List Items

by suresh

Python count List is one of the List methods used to count how many times an item repeated in a given list. In this section, we discuss how to use this List count function with practical examples.

The syntax of the list count function is

list_name.count(list_item)

The python count function counts the total number of times the item repeated in a given list. The below code count 10 and 20 in an integer list.

# Python Count List Items

a = [10, 20, 30, 10, 40, 10, 50, 20]

print("Total Number of Times 10 has repeated = ", a.count(10))
print("Total Number of Times 20 has repeated = ", a.count(20))

OUTPUT

Python Count List Function 1

Python count List Example

In this example, we declared a string list. Next, we used the count function on it.

TIP: Please refer to List article and List methods article to understand everything about Lists in Python.

# Python Count List Items

Fruits = ['Apple', 'Orange', 'Banana', 'Apple', 'Grape', 'Banana', 'Apple']

print("Total Number of Times 'Apple' has repeated = ", Fruits.count('Apple'))
print("Total Number of Times 'Banana' has repeated = ", Fruits.count('Banana'))

OUTPUT

Python Count List Function 2

Let me use this count function on Mixed List.

# Python Count List Items

MxList = ['Apple', 10, 'Banana', 10, 'Apple', 'Grape', 10, 30, 10, 50, 'Apple']

print("Total Number of Times 'Apple' has repeated = ", MxList.count('Apple'))
print("Total Number of Times 10 has repeated = ", MxList.count(10))

OUTPUT

Python Count List Function 3

This time, we used Python List count function on the Nested list (list inside a list).

# Python Count List Items

MxList = [[10, 20], [20, 30], [10, 20], [40, 50], [10, 80]]

print("Total Number of Times [10,20] has repeated = ", MxList.count([10,20]))

OUTPUT

Python Count List Function 4

List count Function Example 2

This python program is the same as the first example. However, this time, we are allowing the user to enter the length of a List. Next, we used For Loop to append those numbers to the list.

# Python Count List Items

intCountList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intCountList.append(value)
    
item = int(input("Please enter the Item that you want to Count: "))
print("Total Number of Times has repeated = ", intCountList.count(item))

OUTPUT

Python Count List Function 5

List count Function Example 3

This program allows users to enter their own string or words and then count the word

# Python Count List Items

strCountList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = input("Please enter the Value of %d Element : " %i)
    strCountList.append(value)
    
item = input("Please enter the Item that you want to Count: ")
print("Total Number of Times has repeated = ", strCountList.count(item))

OUTPUT

Python Count List Function 6

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy