views:

109

answers:

1

I have a list of dictionaries that have the same keys within eg:

[{k1:'foo', k2:'bar', k3...k4....}, {k1:'foo2', k2:'bar2', k3...k4....}, ....]

I'm trying to delete k1 from all dictionaries within the list.

I tried

map(lambda x: del x['k1'], list)

but that gave me a syntax error. Where have I gone wrong?

+2  A: 

lambda bodies are only expressions, not statements like del.

If you have to use map and lambda, then:

map(lambda d: d.pop('k1'), list_of_d)

A for loop is probably clearer:

for d in list_of_d:
    del d['k1']
Ned Batchelder
wont that just give me a list of 'k1's? pop returns the value that gets removed i believe.
webley
It sounds like you don't really want `map`. You are not trying to compute a list of values from another list. You are trying to act on a list. Use the for loop instead.
Ned Batchelder
And BTW, `map` will produce the list of deleted values, but you can ignore the value returned from `map` if you want.
Ned Batchelder
sorry misunderstood you - just tried it and yes the list has k1 removed. will take your suggestion and go with the list.
webley