Count value in list Python

Contents

  • Introduction
  • Syntax count[]
  • Example 1: Count occurrences of Item in Python List
  • Summary

Count Items in List with a Given Value

To count the number of occurrences of items with a specific value in a list, you can call count[] function on the list with the specific item passed as argument to the count[] function.

Syntax count[]

Following is the syntax of count[] function.

n = list.count[item]Run

count[] function returns the number of occurrences of the item in this list.

Example 1: Count occurrences of Item in Python List

In the following example, we will create a list and find the number of occurrences of some of the items in the list.

Python Program

mylist = [6, 52, 74, 62, 85, 62, 62, 85, 6, 92, 74] length_74 = mylist.count[74] length_62 = mylist.count[62] length_92 = mylist.count[92] print['74 occurred', length_74, 'times in the list.'] print['62 occurred', length_62, 'times in the list.'] print['92 occurred', length_92, 'times in the list.']Run

Output

74 occurred 2 times in the list. 62 occurred 3 times in the list. 92 occurred 1 times in the list.

The count[] function takes exactly one argument.

Summary

In this tutorial of Python Examples, we learned how to count the number of times an item is present in a given list.

Related Tutorials

  • Python List with First N Elements
  • Python List of Strings
  • Python Program to Find Smallest Number in List
  • How to Check if Python List is Empty?
  • Python List of Dictionaries
  • How to Reverse Python List?
  • Python List of Functions
  • Python Program to Find Largest Number in a List
  • How to Append List to Another List in Python? list.extend[list]
  • How to Get the list of all Python keywords?

Video liên quan

Chủ Đề