Re: How to iterate on a changing dictionary

2011-06-21 Thread MRAB
On 21/06/2011 12:51, Gurpreet Singh wrote: Perhaps this is the simplest and best solution which appears in this case. Just copy the desired items to a new dictionary and discard the original one. import re myDict={'a':'alpha','b':'beta','c':'charley','d':'disney'} myNewDict={} for k,v in myDict

Re: How to iterate on a changing dictionary

2011-06-21 Thread Gurpreet Singh
Perhaps this is the simplest and best solution which appears in this case. Just copy the desired items to a new dictionary and discard the original one. import re myDict={'a':'alpha','b':'beta','c':'charley','d':'disney'} myNewDict={} for k,v in myDict.iteritems(): if re.search("a",v)!=None:

Re: How to iterate on a changing dictionary

2011-06-21 Thread TheSaint
Terry Reedy wrote: > Other situations will need other solutions. > Like a job's completion list. Some number of workers get a job, and by time the caller sould know who and what has finished. Then a dictionary would hold number of remaining jobs. Similar a downloading list. -- goto /dev/null

Re: How to iterate on a changing dictionary

2011-06-20 Thread Terry Reedy
On 6/20/2011 10:30 AM, Florencio Cano wrote: To make an example: imaging Bingo.Shuffle the numbers, each number sorted should be removed from the container, how would it implemented? The structure seems a set -> unordered collection of unique elements. You can select a random element from the

Re: How to iterate on a changing dictionary

2011-06-20 Thread Florencio Cano
> To make an example: imaging Bingo.Shuffle the numbers, each number sorted > should be removed from the container, how would it implemented? The structure seems a set -> unordered collection of unique elements. You can select a random element from the set with random.sample(container, num_of_ele

Re: How to iterate on a changing dictionary

2011-06-20 Thread TheSaint
Lie Ryan wrote: Thank you all for the information, really apreciated. > While there are legitimate reasons for iterating a dictionary, I'd > consider the alternatives first. Perhaps the correct answer is in what you said. For certain reasons, searching in a dictionary is the fastest method, se

Re: How to iterate on a changing dictionary

2011-06-19 Thread Lie Ryan
On 06/20/11 00:32, TheSaint wrote: > Hello > > Trying to pop some key from a dict while is iterating over it will cause an > exception. > How I can remove items when the search result is true. > > Example: > > while len(dict): >for key in dict.keys(): > if dict[key] is not my_result:

Re: How to iterate on a changing dictionary

2011-06-19 Thread Terry Reedy
On 6/19/2011 11:53 AM, Roy Smith wrote: Yet another variation which makes sense if you want to delete most of the keys would be to copy them to a new dictionary. I'm not sure how Python handles memory management on dictionaries which shrink. 'Python' does not handle memory management; each im

Re: How to iterate on a changing dictionary

2011-06-19 Thread Terry Reedy
On 6/19/2011 11:13 AM, Chris Angelico wrote: On Mon, Jun 20, 2011 at 12:32 AM, TheSaint wrote: Hello Trying to pop some key from a dict while is iterating over it will cause an exception. How I can remove items when the search result is true. Example: while len(dict): for key in dict.keys

Re: How to iterate on a changing dictionary

2011-06-19 Thread Roy Smith
In article , Chris Angelico wrote: > On Mon, Jun 20, 2011 at 12:32 AM, TheSaint wrote: > > Hello > > > > Trying to pop some key from a dict while is iterating over it will cause an > > exception. > > How I can remove items when the search result is true. > > > > Example: > > > > while len(dict)

Re: How to iterate on a changing dictionary

2011-06-19 Thread Chris Angelico
On Mon, Jun 20, 2011 at 12:32 AM, TheSaint wrote: > Hello > > Trying to pop some key from a dict while is iterating over it will cause an > exception. > How I can remove items when the search result is true. > > Example: > > while len(dict): >   for key in dict.keys(): >      if dict[key] is not m

How to iterate on a changing dictionary

2011-06-19 Thread TheSaint
Hello Trying to pop some key from a dict while is iterating over it will cause an exception. How I can remove items when the search result is true. Example: while len(dict): for key in dict.keys(): if dict[key] is not my_result: dict.pop(key) else: condition_to_brea