Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7986
| Date | Mon, 20 Jun 2011 03:00:27 +1000 |
|---|---|
| From | Lie Ryan <lie.1296@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110513 Lightning/1.0b3pre Thunderbird/3.1.10 ThunderBrowse/3.3.5 |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.python |
| Subject | Re: How to iterate on a changing dictionary |
| References | <itl1a5$rom$1@speranza.aioe.org> |
| In-Reply-To | <itl1a5$rom$1@speranza.aioe.org> |
| X-Enigmail-Version | 1.1.2 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| NNTP-Posting-Host | 110.175.240.90 |
| X-Original-NNTP-Posting-Host | 110.175.240.90 |
| Message-ID | <4dfe2b95$1@dnews.tpgi.com.au> (permalink) |
| X-Trace | dnews.tpgi.com.au!tpg.com.au 1308502933 110.175.240.90 (20 Jun 2011 03:02:13 +1000) |
| Lines | 28 |
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.alt.net!news.kjsl.com!rahul.net!wasp.rahul.net!rahul.net!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!nntp3.phx1!dnews.tpgi.com.au!tpg.com.au!not-for-mail |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:7986 |
Show key headers only | View raw
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:
> dict.pop(key)
> else:
> condition_to_break
> print('Dictionary is over')
Others has described how to do what you wanted to do, but let's address
the main problem here, why are you iterating a dictionary?
I found that most of the time that I thought I needed to iterate through
a dictionary, it's really because I'm thinking the wrong way, and a
little bit more thought lead me to a better way that doesn't involve
iterating on the dictionary.
While there are legitimate reasons for iterating a dictionary, I'd
consider the alternatives first.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-19 22:32 +0800
Re: How to iterate on a changing dictionary Chris Angelico <rosuav@gmail.com> - 2011-06-20 01:13 +1000
Re: How to iterate on a changing dictionary Roy Smith <roy@panix.com> - 2011-06-19 11:53 -0400
Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:51 -0400
Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-19 12:02 -0400
Re: How to iterate on a changing dictionary Lie Ryan <lie.1296@gmail.com> - 2011-06-20 03:00 +1000
Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-20 21:20 +0800
Re: How to iterate on a changing dictionary Florencio Cano <florencio.cano@gmail.com> - 2011-06-20 16:30 +0200
Re: How to iterate on a changing dictionary Terry Reedy <tjreedy@udel.edu> - 2011-06-20 13:37 -0400
Re: How to iterate on a changing dictionary TheSaint <nobody@nowhere.net.no> - 2011-06-21 18:44 +0800
csiph-web