This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: list.count() results wrong if both booleans & 1's/0's are present in the list
Type: behavior Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: alex, loewis, varun_masuraha
Priority: normal Keywords:

Created on 2012-09-02 16:25 by varun_masuraha, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg169707 - (view) Author: Varun Masuraha (varun_masuraha) Date: 2012-09-02 16:25
setup:
>>>myList=[1,True,'blah blah']
>>>print(myList.count(1))

Actual result:
2

Expected:
1
msg169708 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2012-09-02 16:30
list.count() uses == to compare items, 1 and True compare equal, so this behavior is correct.
msg169716 - (view) Author: Varun Masuraha (varun_masuraha) Date: 2012-09-02 18:23
Thanks Alex for the reply,

Although Booleans & Integers may not be very practical in same list (or tuple, etc.) & the fact that bool is subclass of int, still I feel that the search implementation (count, index, etc.) is too simple in that matter.
Comparing both Value AND Data type while searching, looks more exact to me...

regards,
Varun
msg169747 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-09-03 05:51
Varun: what do you think that

py> x = [1.0, 2.0, 3.0]
py> x.count(3)
1

should give? I think most people will agree that there is one single "three" in the list, as 3 and 3.0 are equal in all other contexts as well (as is 3.0+0j). If types were considered, this would give 0.
msg169751 - (view) Author: Varun Masuraha (varun_masuraha) Date: 2012-09-03 08:46
Hey Martin,

I think I got the point.
Meanwhile I created my own myCount() for my problem ;-)

Thanks.
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60054
2012-09-03 08:46:25varun_masurahasetmessages: + msg169751
2012-09-03 05:51:03loewissetnosy: + loewis
messages: + msg169747
2012-09-02 18:23:43varun_masurahasetmessages: + msg169716
2012-09-02 16:30:43alexsetstatus: open -> closed

nosy: + alex
messages: + msg169708

resolution: not a bug
2012-09-02 16:25:55varun_masurahacreate