[Tutor] Removing values from a dictionary if they are present in alist

Alan Gauld alan.gauld at btinternet.com
Fri Apr 1 10:10:24 CEST 2011


"ranjan das" <ranjand2005 at gmail.com> wrote

> A={'g2': [4,5,3], 'g1': [1, 3]}
>
> B=[2,3,5]
>
> Now I want to remeove the elements in B if they are present (as 
> values) in
> dictionary A.

My first thought was to start with the dictionary, something like

for key in A:
     A[key] = [val for val in A[key] if val not in B]

Dunno what the efficiency will be like because there are a lot of 
hidden
loops going on - but at least they are in C not Python. But I don't 
think
it would be much worse than your option...

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list