"Slice" out one element from a python dictionary

Posted by BCS on Stack Overflow See other posts from Stack Overflow or by BCS
Published on 2010-06-02T19:06:53Z Indexed on 2010/06/02 19:34 UTC
Read the original article Hit count: 144

Filed under:
|

I have a dictionary:

D = { "foo" : "bar", "baz" : "bip" }

and I want to create new dictionary that has a copy of one of it's elements k. So if k = "baz":

R = { "baz" : "bip" }

what I'v got now is:

R = { k : D[k] }

But in my case k is a complex expression and I've got a whole stack of these. Caching k in a temporary looks about as ugly as the original option.

What I'm looking for is a better (cleaner) way to do this.

© Stack Overflow or respective owner

Related posts about python

Related posts about dictionary