Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-09 Thread Mats Wichmann
On 9/8/20 5:18 PM, Richard Damon wrote: > On 9/8/20 7:06 PM, Mats Wichmann wrote: >> On 9/7/20 5:01 PM, Driuma Nikita wrote: >> for i, el in enumerate(_list[:]): >> del _list[i] >> > The issue isn't so much that he is modifying the list that he is > iterating over, but also when he de

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-09 Thread Chris Angelico
On Wed, Sep 9, 2020 at 6:18 PM Nicholas Cole wro > > On Wed, Sep 9, 2020 at 8:52 AM Chris Angelico wrote: > > [snip] > > And if you absolutely have to mutate in place: > > > > items[:] = [i for i in items if i not in "bcd"] > > How does that work to mutate in place? Technically it constructs a n

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-09 Thread Nicholas Cole
On Wed, Sep 9, 2020 at 8:52 AM Chris Angelico wrote: [snip] > And if you absolutely have to mutate in place: > > items[:] = [i for i in items if i not in "bcd"] How does that work to mutate in place? -- https://mail.python.org/mailman/listinfo/python-list

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-09 Thread Chris Angelico
On Wed, Sep 9, 2020 at 5:45 PM Peter Otten <__pete...@web.de> wrote: > > Peter Otten wrote: > > > If the list is huge you can also delete in reverse order: > > > > for i in reversed(len(_list)): > > Make that reversed(range(len(_list))). > > > if discard(_list[i]): > > del _list[i] > >

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-09 Thread Peter Otten
Peter Otten wrote: > If the list is huge you can also delete in reverse order: > > for i in reversed(len(_list)): Make that reversed(range(len(_list))). > if discard(_list[i]): > del _list[i] Example: >>> items = ['a', 'b', 'c', 'd', 'e'] >>> for i, item in enumerate(items): ...

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-08 Thread Peter Otten
Mats Wichmann wrote: > On 9/7/20 5:01 PM, Driuma Nikita wrote: > > > _list = list(range(50)) > for i, el in enumerate(_list): > del _list[i] > print(_list) > > > Don't change the the list while you are iterating over it, it messes up > the iteration. It's not "randomly

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-08 Thread Richard Damon
On 9/8/20 7:06 PM, Mats Wichmann wrote: > On 9/7/20 5:01 PM, Driuma Nikita wrote: > > > _list = list(range(50)) > for i, el in enumerate(_list): > del _list[i] > print(_list) > > > Don't change the the list while you are iterating over it, it messes up > the iteration. It's

Re: Fwd: PYTHON BUG. deleting elements of list.

2020-09-08 Thread Mats Wichmann
On 9/7/20 5:01 PM, Driuma Nikita wrote: _list = list(range(50)) for i, el in enumerate(_list): del _list[i] print(_list) Don't change the the list while you are iterating over it, it messes up the iteration. It's not "randomly deleting", it's when next is called to fetch

Fwd: PYTHON BUG. deleting elements of list.

2020-09-08 Thread Driuma Nikita
    Пересылаемое сообщение 08.09.2020, 01:43, "python-list-ow...@python.org" :   Hello! I see you want to post a message to the Python List. We would be happy to help, but you must subscribe first:   [1]https://mail.python.org/mailman/listinfo/python-l