Newbie question: eliminating entries in dict

sean sean_berry at cox.net
Tue Nov 25 21:20:20 EST 2003


I had tried something very similar to that but ended up with the same error
I am getting now.

Traceback (most recent call last):
  File "C:\Python23\trial.py", line 93, in -toplevel-
    d = ([(k,v) for k, v in a.iteritems() if (k.split("*", 1)[1]) not in b])
IndexError: list index out of range

Changing the [1] to a [0] makes the Error go away, but compares the wrong
part of the entry.

Any ideas?


"sean" <sean_berry at cox.net> wrote in message
news:avSwb.7451$9O5.1937 at fed1read06...
> I have two dictionaries.
>
> a = {'a1': 1, 'a2': 5, 'a3': 2, 'a4': 7}
> b = {'something*a4': 1, 'something*something': 1,
> 'somethingelse*somethingelse': 1, 'something*a1: 1}
>
> What would be an efficient way of eliminating all entries in b which
contain
> an entry from a.
>
> Such that the result would be
>
> c = ['something*something', 'somethingelse*somethingelse']
>
> or better still
> c = {'something*something': 1, 'something*something': 1}
>
> I tried a few different approaches, but they seem very long and
unnecessary.
> Also searched the mailing list archives and newsgroups and could not come
up
> with anything.
>
> I know that I will need to compare the .split("*", 1)[1] part of b with
> those from a, but am not sure
> how to implement it.
>
> Thanks in advance for any help
>
>






More information about the Python-list mailing list