how can i count the items in a list?

+1 vote
can you give an example using a sample list having numbers from 0-10.
May 2 in Python by Waseem
• 4,480 points
226 views

2 answers to this question.

+1 vote
suppose you have a list

a = [0,1,2,3,4,5,6,7,8,9,10]

now if you want to count the items in your list. you should have a value at your disposal

suppose you want to check the count the number of times 2 is in your list.

a.count(2)

it will give you the count.

In this case if you want the number of items in your list you can just check the length

len(a)

it will give you the number of items in your list.
answered May 2 by Mohammad
• 3,060 points
+2 votes

Syntax :

           list. count(value)

Code:

colors = ['red', 'green', 'blue', 'black']

Return the number of times the value' blue' appears int in the colors list

x= colors.count("blue")
print(x)
Output: 1
answered Jul 6 by Neha
• 330 points

edited Jul 8 by Kalgi

Related Questions In Python

0 votes
1 answer

How can I get the count of a list in Python?

len() >>> mylist=[] >>> print len(mylist) 0 READ MORE

answered Dec 12, 2018 in Python by charlie_brown
• 7,720 points
67 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
62 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
672 views
0 votes
1 answer

How can I lookup hostname using the IP address with a timeout in Python?

Good question. I actually was stuck with ...READ MORE

answered Feb 6 in Python by Nymeria
• 3,520 points
69 views