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
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
> -- 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
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
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
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
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 =
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
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
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
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
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 changing, but how would I str
12 matches
Mail list logo