Take the 2-minute tour ×

In Python what is the most efficient way to do this:

my_var = some_var['my_key'] | None

ie. assign some_var['my_key'] to my_var if some_var contains 'my_key', otherwise make my_var be None.

share|improve this question
What do you mean by "if it exists"? May the name some_var be undefined in some cases? –  Sven Marnach Feb 14 '12 at 22:22
I just edited it - to clarify - i'm trying to check for the existence of a key in a dictionary essentially –  9-bits Feb 14 '12 at 22:24
I bet this has already been asked... –  Oleh Prypin Feb 14 '12 at 22:27
See my answer for the very useful second argument to dict.get() which nobody seems to be mentioning... –  Endophage Feb 14 '12 at 22:28
add comment

6 Answers

up vote 3 down vote accepted

Python will throw a NameError if the variable doesn't exist so you can't write your code in quite the same way as your JavaScript. However, if you are operating specifically with dicts as in your example, there is a very nice function mydict.get('key', default) which attempts to get the key from the dictionary and returns the default value if the key doesn't exist.

If you just want to default to be None you don't need to explicitly pass the second argument.

share|improve this answer
add comment

Assuming some_var is a dictionary, you need dict.get():

my_var = some_var.get('my_key')

This result defaults to None if my_key is missing, but you can supply a different default:

my_var = some_var.get('my_key', default)
share|improve this answer
add comment

You are looking for the get() method of dict.

my_var = some_var.get('some_key')

The get() method will return the value associated with 'some_key', if such a value exists. If the key is not present, then None will be returned.

share|improve this answer
add comment
    my_var = some_var
    my_var = None

But honestly this probably doesn't get to the heart of what you're trying to do... We need more context to more fully answer.

share|improve this answer
I just added a clarification - some_var is actually a dictionary and i want to ensure the key exists in it otherwise use None –  9-bits Feb 14 '12 at 22:25
add comment

The great thing about the .get() method is you can actually define a value to return in case the key doesn't exist.

my_dict = { 1: 'one', 2: 'two' }
print my_dict.get(3, 'Undefined key')

would print.

Undefined key

This is very helpful not only for debugging purposes, but also when parsing json (in my experience, at least), and you should prefer using get() over [] as much as possible.

share|improve this answer
add comment

In python "|" is translated to "or", so:

my_var = some_var or None

Edit: You've edited your initial post. The correct way to do what you want is:

my_var = some_var.get('my_key', None)
share|improve this answer
None is useless, default is None --> docs.python.org/library/stdtypes.html#typesmapping –  user1125315 Feb 14 '12 at 22:44
It's just an example to illustrate the use of dict.get. –  user1179901 Feb 15 '12 at 16:59
add comment

Your Answer


