[issue40809] list.Count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread Steven D'Aprano
Steven D'Aprano added the comment: Typo: "on each step, you delete one item from the from and step forward" Should be, you delete one item from the FRONT and step forward. -- ___ Python tracker

[issue40809] list.Count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread Steven D'Aprano
Steven D'Aprano added the comment: Rémi is correct, this is not a bug. The problem isn't with list.count. If you print list.count each time through the loop, you will see that it is working perfectly. The problem is that you are modifying the list as you are iterating over it. That means

[issue40809] list.Count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread chirag maliwal
chirag maliwal added the comment: a = [0, 1, 2, 3, 4, 1, 2, 3, 5] for i in a: if a.count(i) > 1: a.remove(i) print(a) """ Output [0, 4, 1, 2, 3, 5] """ It's working fine for the above code. Yes I can use set for this but I tried with count and remove. --

[issue40809] list.Count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread Rémi Lapeyre
Rémi Lapeyre added the comment: Hi cmaliwal! You are mutating the list while iterating over it which can lead to all sorts of issues, to keep the unique elements of a list you can do >>> a = [1, 1, 1, 1] >>> list(set(a)) [1] If you think you found a bug in Python please check in the

[issue40809] list.count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread chirag maliwal
Change by chirag maliwal : -- title: list.Count() isn't working as expected for the series of same numbers in a list -> list.count() isn't working as expected for the series of same numbers in a list ___ Python tracker

[issue40809] list.Count() isn't working as expected for the series of same numbers in a list

2020-05-28 Thread chirag maliwal
New submission from chirag maliwal : a = [1,1,1,1] for i in a: if a.count(i) > 1: a.remove(i) print(a) """ Output: [1,1] expected: [1] """ -- files: test.py messages: 370239 nosy: cmaliwal priority: normal severity: normal status: open title: list.Count() isn't