[Chicago] Deleting from a list or dictionary while processing it?

Dan O'Day 4n6.listserv at gmail.com
Mon Mar 27 13:26:52 EDT 2017


Just be careful if relying on specific list indices after that, as the
elements' positions will change.

Dan


On Sat, Mar 25, 2017 at 5:07 PM, Jordan Bettis <jordanb at hafd.org> wrote:
> If you're trying to use a list as a stack you can just use list.pop:
>
> In [1]: my_stack = [1,2,3,4,5,6]
>
> In [2]: while len(my_stack) > 0:
>    ...:     print(l.pop())
>    ...:
> 6
> 5
> 4
> 3
> 2
> 1
>
>
> On 03/25/2017 04:59 PM, Lewit, Douglas wrote:
>
> Hi guys,
>
> I remember doing some of this once upon a time, but I may have forgotten the
> details, so if someone could refresh my memory that would be great.
>
> In essence, what I'm trying to do is process a list ( or perhaps a
> dictionary ) BUT WHILE I'M PROCESSING IT I WANT TO MUTATE IT BY DELETING
> ELEMENTS FROM IT.  Does that make sense?
>
> ( Launching Python interpreter to provide a concrete example! )
>
> while my_list:
>
>       for n in my_list[:]:
>
>             print(n)
>
>             my_list = my_list[1:]
>
>
> I think that's correct.... but still.... it can get a bit confusing.  Okay,
> thanks for the feedback.
>
>
> Best,
>
>
> Douglas.
>
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list