I am trying to count the output in python. 'len' seems to be not working with the output. Following is my code
for i in range [0, 5]:
print i
The output is
0
1
2
3
4
I want to print the count of 'i' which should give me count of '5'
Thank you in advance!
The6thSense
7,7536 gold badges30 silver badges63 bronze badges
asked Aug 20, 2015 at 8:45
0
Use enumerate
to count the iterations:
count = 0
for count, i in enumerate[range[5],1]:
print[i]
print[count]
If you want the size of a list use len:
print[len[range[5]]]
answered Aug 20, 2015 at 8:48
1
There are many ways to do but my two ways would be
One way is to use len
i =len[range [0, 5]]
print i
Other way is using for:
j=0
for i in range [0, 5]:
#Other things you want to do
j+=1
print j
answered Aug 20, 2015 at 8:49
The6thSenseThe6thSense
7,7536 gold badges30 silver badges63 bronze badges
2
The count[] is a built-in function in Python. It will return the total count of a given element in a list. The count[] function is used to count elements on a list as well as a string.
In this Python tutorial, you will learn:
- Python count
- Python List count[]
- Example 1: List Count
- Example 2: Find the count of elements [Duplicates] in a givenlist
Python List count[]
The count[] is a built-in function in Python. It will return you the count of a given element in the list.
Syntax:
list.count[element]
Parameters:
element: The element you want to find the count.
ReturnValue:
The count[] method will return an integer value, i.e., the count of the given element from the given list. It returns a 0 if the value is not found in the given list.
Example 1: List Count
Following example shows the working of count[] function on a list:
list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green'] color_count = list1.count['green'] print['The count of color: green is ', color_count]
Output:
The count of color: green is 3
Example 2: Find the count of elements [Duplicates] in a givenlist
list1 = [2,3,4,3,10,3,5,6,3] elm_count = list1.count[3] print['The count of element: 3 is ', elm_count]
Output:
The count of element: 3 is 4
Summary:
- The count[] is a built-in function in Python. It will return you the count of a given element in a list or a string.
- In the case of a list, the element to be counted needs to be given to the count[] function, and it will return the count of the element.
- The count[] method returns an integer value.
In this tutorial, we will learn about the Python List count[] method with the help of examples.
The count[]
method returns the number of times the specified element appears in the list.
Example
# create a list
numbers = [2, 3, 5, 2, 11, 2, 7]
# check the count of 2
count = numbers.count[2]
print['Count of 2:', count]
# Output: Count of 2: 3
Syntax of List count[]
The syntax of the count[]
method is:
list.count[element]
count[] Parameters
The count[]
method takes a single
argument:
- element - the element to be counted
Return value from count[]
The count[]
method returns the number of times element appears in the list.
Example 1: Use of count[]
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# count element 'i'
count = vowels.count['i']
# print count
print['The count of i is:', count]
# count element 'p'
count = vowels.count['p']
# print count
print['The count of p is:', count]
Output
The count of i is: 2 The count of p is: 0
Example 2: Count Tuple and List Elements Inside List
# random list
random = ['a', ['a', 'b'], ['a', 'b'], [3, 4]]
# count element ['a', 'b']
count = random.count[['a', 'b']]
# print count
print["The count of ['a', 'b'] is:", count]
# count element [3, 4]
count = random.count[[3, 4]]
# print count
print["The count of [3, 4] is:", count]
Output
The count of ['a', 'b'] is: 2 The count of [3, 4] is: 1