Python Forum
list.count does not appear to function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: list.count does not appear to function (/thread-32915.html)



list.count does not appear to function - Oldman45 - Mar-16-2021

I am learning about lists and apparently list.count does not function in the following:
mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
mylist.count(3)
print(mylist)
The output I get is
Output:
[5, 12, 27, 3, 12, 5, 9, 5, 11]
whereas it should return 1
When I use .insert, .reverse, .extend all is well!

Am I likely to have a problem with the Python package I am using?

I am using Windows 10 and Python 3.9.2

Any help would be appreciated


RE: list.count does not appear to function - sandeep_ganga - Mar-16-2021

It is working good to me,

mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
print(mylist.count(5))
print(mylist.count(3))
print(mylist)

k=mylist.count(12)
print(k)
Output:
C:\Users\sandeep\Downloads>py test.py 3 1 [5, 12, 27, 3, 12, 5, 9, 5, 11] 2
Best Regards,
Sandeep.

GANGA SANDEEP KUMAR


RE: list.count does not appear to function - snippsat - Mar-16-2021

mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
print(mylist.count(3)) # Need print()
print(mylist)
Output:
1 [5, 12, 27, 3, 12, 5, 9, 5, 11]
mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
count_number = 5
result = mylist.count(count_number)
print(f'There where {result} occurrences of <{count_number}> in:\n{mylist}')
Output:
There where 3 occurrences of <5> in: [5, 12, 27, 3, 12, 5, 9, 5, 11]



RE: list.count does not appear to function - buran - Mar-16-2021

It is important that you understand that there are methods that work in-place (and return None) and others that return something like in this case - list.count() will return the result (if you want to use it, you need to bind it to a name, or print it, etc.)


RE: list.count does not appear to function - Oldman45 - Mar-16-2021

Thank you Sandeep_ganga, snippsat & Buran.
I get that - I was not using the print statement correctly not recognising I had called up print the original list and not the count result. However if I use mylist.reverse I have to revert to the my initial way of working as belowL
mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
mylist.reverse()
print(mylist)
which gives the correct output of
Output:
[11, 5, 9, 5, 12, 3, 27, 12, 5]
Is this because I am using () in the call and not specifying anything between ()?

Thanks


RE: list.count does not appear to function - buran - Mar-16-2021

(Mar-16-2021, 01:42 PM)Oldman45 Wrote: Is this because I am using () in the call and not specifying anything between ()?
no, it is because .reverse() method works in-place.


RE: list.count does not appear to function - snippsat - Mar-16-2021

(Mar-16-2021, 01:42 PM)Oldman45 Wrote: Is this because I am using () in the call and not specifying anything between ()?
No it's because reverse() is modifying the original list in place.
So in case can not store it in a variable.
>>> mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
>>> help(mylist.reverse)
Help on built-in function reverse:

reverse() method of builtins.list instance
    Reverse *IN PLACE*.

>>> rev = mylist.reverse()
>>> rev
>>> repr(rev)
'None'
>>> 
>>> mylist
[11, 5, 9, 5, 12, 3, 27, 12, 5]
count() is different as it return the result that can be stored in variable.
>>> mylist
[11, 5, 9, 5, 12, 3, 27, 12, 5]
>>> mylist = [5, 12, 27, 3, 12, 5, 9, 5, 11]
>>> help(mylist.count)
Help on built-in function count:

count(value, /) method of builtins.list instance
    Return number of occurrences of value.

>>> c = mylist.count(5)
>>> c
3 



RE: list.count does not appear to function - Oldman45 - Mar-16-2021

Buran, snippsat thanks for the education, I will now practice what I learnt. I really appreciate the support