Iterating over dict and removing some elements

Jerry Hill malaclypse2 at gmail.com
Tue May 11 11:29:34 EDT 2010


On Tue, May 11, 2010 at 11:08 AM, Ulrich Eckhardt
<eckhardt at satorlaser.com> 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 in d.keys():
    if d[k] == 'two':
        d.pop(k)


-- 
Jerry



More information about the Python-list mailing list