Re: dictionary size changed during iteration

2011-05-08 Thread Paul Rubin
Hans Mulder writes: > How about: > changes = filter(is_bad, d) > Or would that be too compact? I thought of writing something like that but filter in python 3 creates an iterator that would have the same issue of walking the dictionary while the dictionary is mutating. changes = list(f

Re: dictionary size changed during iteration

2011-05-08 Thread Hans Mulder
On 08/05/2011 00:12, Roy Smith wrote: In article<7xd3jukyn9@ruckus.brouhaha.com>, Paul Rubin wrote: Roy Smith writes: changes = [ ] for key in d.iterkeys(): if is_bad(key): changes.append(key) changes = list(k for k in d if is_bad(k)) is a little bit more direct. This

Re: dictionary size changed during iteration

2011-05-07 Thread Roy Smith
In article <7xd3jukyn9@ruckus.brouhaha.com>, Paul Rubin wrote: > Roy Smith writes: > > changes = [ ] > > for key in d.iterkeys(): > > if is_bad(key): > > changes.append(key) > > changes = list(k for k in d if is_bad(k)) > > is a little bit more direct. This is true. I still fi

Re: dictionary size changed during iteration

2011-05-07 Thread Paul Rubin
Roy Smith writes: > changes = [ ] > for key in d.iterkeys(): > if is_bad(key): > changes.append(key) changes = list(k for k in d if is_bad(k)) is a little bit more direct. -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionary size changed during iteration

2011-05-07 Thread Laszlo Nagy
... That works, but if d is large, it won't be very efficient because it has to generate a large list. It is not large. But I'm using Python 2.6 , not Python 3. I did not get this error again in the last two days. I'll post a new reply if I encounter it again. (It happened just a few times o

Re: dictionary size changed during iteration

2011-04-22 Thread Roy Smith
In article , Peter Otten <__pete...@web.de> wrote: > You now have to create the list explicitly to avoid the error: > > >>> d = dict(a=1) > >>> keys = list(d.keys()) > >>> for k in keys: > ... d["b"] = 42 > ... That works, but if d is large, it won't be very efficient because it has to gen

Re: dictionary size changed during iteration

2011-04-22 Thread Mark Niemczyk
As of Python 3.x (which I suspect you are running): "The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.", and "Iterating vi

Re: dictionary size changed during iteration

2011-04-21 Thread John Nagle
On 4/20/2011 5:52 AM, Laszlo Nagy wrote: Given this iterator: class SomeIterableObject(object): def __iter__(self): ukeys = self.updates.keys() for key in ukeys: if self.updates.has_key(key): yield self.updates[key] for rec in self.inserts: yield rec How can I get this exce

Re: dictionary size changed during iteration

2011-04-20 Thread Mel
Mel wrote: > Laszlo Nagy wrote: > `ukeys` isn't a different dictionary from `self.updates.keys` I'ts merely > another name referring to the same dict object. I think > > ukeys = dict (self.updates.keys) > > would do what you want. Sorry. Belay that. Thought I'd had enough coffee. Me

Re: dictionary size changed during iteration

2011-04-20 Thread Mel
Laszlo Nagy wrote: > Given this iterator: > > class SomeIterableObject(object): > > > > def __iter__(self): > ukeys = self.updates.keys() > for key in ukeys: > if self.updates.has_key(key): > yield self.updates[key] >

Re: dictionary size changed during iteration

2011-04-20 Thread Peter Otten
Peter Otten wrote: > Laszlo Nagy wrote: > >> Given this iterator: >> >> class SomeIterableObject(object): >> >> >> >> def __iter__(self): >> ukeys = self.updates.keys() >> for key in ukeys: >> if self.updates.has_key(key): Hm, I see you a

Re: dictionary size changed during iteration

2011-04-20 Thread Peter Otten
Laszlo Nagy wrote: > Given this iterator: > > class SomeIterableObject(object): > > > > def __iter__(self): > ukeys = self.updates.keys() > for key in ukeys: > if self.updates.has_key(key): > yield self.updates[key] >

dictionary size changed during iteration

2011-04-20 Thread Laszlo Nagy
Given this iterator: class SomeIterableObject(object): def __iter__(self): ukeys = self.updates.keys() for key in ukeys: if self.updates.has_key(key): yield self.updates[key] for rec in self.inserts: yield rec