Re: Iterating over dict and removing some elements

2010-05-14 Thread Paul Rubin
Bryan writes: > In Python 3.X, and in Python 2.X starting with 2.4, you can drop the > square brackets and avoid creating an extra temporary list: > > d = dict((k, d[k]) for k in d.keys() if not foo(k, d)) In 2.x, I think you want d.iterkeys() rather than d.keys() to avoid making a list with all

Re: Iterating over dict and removing some elements

2010-05-14 Thread Bryan
Adi Eyal wrote: > > Bryan: > > Terry Reedy wrote: > > [...] > >> for k in [k for k in d if d[k] == 'two']: > >>          d.pop(k) > > > We have a winner. > > also > > foo = lambda k, d : d[k] == "two" > d = dict([(k, d[k]) for k in d.keys() if not foo(k, d)]) > > incidentally, this is marginally s

Re: Iterating over dict and removing some elements

2010-05-12 Thread Adi Eyal
> -- Forwarded message -- > From: Bryan > To: python-l...@python.org > Date: Tue, 11 May 2010 23:59:29 -0700 (PDT) > Subject: Re: Iterating over dict and removing some elements > Terry Reedy wrote: > [...] >> for k in [k for k in d if d[k] == 'tw

Re: Iterating over dict and removing some elements

2010-05-12 Thread Bryan
Rebelo wrote: > i am wondering why not like this: > >  >>> d = {1: 'one', 2: 'two', 3: 'three'} >  >>> for k,v in d.items(): > ...     if k==1: > ...          del d[k] > ... >  >>> d > {2: 'two', 3: 'three'} >  >>> Mostly because there's no reason to get 'v' if you're not going to use it. That may

Re: Iterating over dict and removing some elements

2010-05-12 Thread Rebelo
On 05/11/2010 05:08 PM, Ulrich Eckhardt wrote: Hi! I wrote a simple loop like this: d = {} ... for k in d: if some_condition(d[k]): d.pop(k) If I run this, Python complains that the dictionary size changed during iteration. I understand that the iterator relies on th

Re: Iterating over dict and removing some elements

2010-05-12 Thread Bryan
Terry Reedy wrote: [...] > for k in [k for k in d if d[k] == 'two']: >          d.pop(k) We have a winner. -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterating over dict and removing some elements

2010-05-11 Thread Terry Reedy
On 5/11/2010 11:29 AM, Jerry Hill wrote: On Tue, May 11, 2010 at 11:08 AM, Ulrich Eckhardt wrote: My first approach was to simply postpone removing the elements, but I was wondering if there was a more elegant solution. Iterate over something other than the actual dictionary, like this: d =

Re: Iterating over dict and removing some elements

2010-05-11 Thread superpollo
superpollo ha scritto: Ulrich Eckhardt ha scritto: Hi! I wrote a simple loop like this: d = {} ... for k in d: if some_condition(d[k]): d.pop(k) If I run this, Python complains that the dictionary size changed during iteration. I understand that the iterator relies on th

Re: Iterating over dict and removing some elements

2010-05-11 Thread Jerry Hill
On Tue, May 11, 2010 at 11:08 AM, Ulrich Eckhardt wrote: > My first approach was to simply postpone removing the elements, but I was > wondering if there was a more elegant solution. Iterate over something other than the actual dictionary, like this: d = {1: 'one', 2: 'two', 3: 'three'} for k i

Re: Iterating over dict and removing some elements

2010-05-11 Thread Michele Simionato
Or you copy the whole dictionary or you just copy the keys: for k in d.keys(): ... or for k in list(d): ... -- http://mail.python.org/mailman/listinfo/python-list

Re: Iterating over dict and removing some elements

2010-05-11 Thread superpollo
Ulrich Eckhardt ha scritto: Hi! I wrote a simple loop like this: d = {} ... for k in d: if some_condition(d[k]): d.pop(k) If I run this, Python complains that the dictionary size changed during iteration. I understand that the iterator relies on the internal structure not