Methods and Functions

Python List count()

The count() in python count items in list. The function returns the number of times a particular element is repeated in a list.  

Syntax:

list.count(element)

count() Parameters

The count() in python takes only one parameter as input from the user.

element- This is the element that must be counted. 

Return value from count()

The count() is used to count how many times a particular element is present in the list. The return value will be the number returned by the count(). 

Example 1: Use of count()

In the program given below, the user wants to count how many times a particular alphabet is repeated in a list.

Source Code

                    

# Given a list of vowels 

vowels = ['a', 'e', 'i', 'o', 'i', 'u', 'a', 'e', 'e']



# count how many times 'i' is repeated

count1 = vowels.count('i')




# print the count

print('The count of 'i' in the list is:', count1)



# count element 'q'

count1 = vowels.count('q')



# print the count of q

print('The count of 'q' in the list is:', count1)

Output

The count of 'i' in the list is: 2

The count of 'q' in the list is: 0

Example 2: Count Tuple and List Elements Inside List

Source Code

                    

# random list

random = ['a', 'b', 'cd', ('a', 'b'),'e', ('a', 'b'), [3, 4]]



# count the element ('a', 'b') is repeated in the list

count = random.count(('a', 'b'))




# print the count

print("count of ('a', 'b') is", count)



# count the element [3, 4] from the list

count = random.count([3, 4])




# print the count

print(" count of [3, 4] is:", count)

Output

count of ('a', 'b') is 2

count of [3, 4] is 1

How do you count all items in a list Python?

In python count items in list using the list comprehension + len() approach. 

Source Code:

                    

list11 = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

  

# printing the original list

print("The original list is: " + str(list11))

 

res = len([ele for sub in list11 for ele in sub])

  

# print the result

print("Total elements is: " + str(res))



Output :

The original list is: [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

Total elements is: 9

How do you count elements in a list?

Another method to count all the elements present in a list is by using the chain() + len()

Source Code:

                    

list11 = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

  

print("The original list is: " + str(list11))

  

res = len(list(chain(*list11)))

  

# print result

print("Total elements is: " + str(res))



Output :

The original list is: [[1, 4, 5], [7, 3], [4], [46, 7, 3]]

Total elements is: 9

How many items can a Python list hold?

On average, a list must include anywhere between 2-8 items. If the list has lesser than 2 items then it will not be considered as a list, it will only be an item. Whereas, if the list has more than 8 items there is a lot of confusion. 

How do you count words in a list in Python?

In python, the user can count the number of words present in a list using the for loop.

Source Code:

                    

carlist11 = ["BMW", "BMW", "JAGUAR", "Audi", "BMW", "Mercedez", "BMW", "Ferrari"]



count = 0



for cars in carlist11:

    if cars == "Audi":

        count = count + 1

print("Total Audi Cars in list are:-", count)



Output:



Total Audi Cars in list are: 1

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.