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

Jordan Bettis jordanb at hafd.org
Sat Mar 25 18:07:02 EDT 2017


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20170325/270f898d/attachment.html>


More information about the Chicago mailing list