The most Pythonic way of checking if a value in a

2019-02-06 03:13发布

Say I have a dictionary, and I want to check if a key is mapped to a nonempty value. One way of doing this would be the len function:

mydict = {"key" : "value", "emptykey" : ""}
print "True" if len(mydict["key"]) > 0 else "False"  # prints true
print "True" if len(mydict["emptykey"]) > 0 else "False"  # prints false

However, one can rely on the semantics of Python and how if an object is defined it evaluates to true and leave out the len call:

mydict = {"key" : "value", "emptykey" : ""}
print "True" if mydict["key"] else "False"  # prints true
print "True" if mydict["emptykey"] else "False"  # prints false

However, I'm not sure which is more Pythonic. The first feels "explicit is better than implicit", however the second feels "simple is better than complex".

I also wonder if the leaving out the len call could bite me as the dict I'm working with doesn't necessarily contain strings, but could contain other len-able types (lists, sets, etc). OTOH, in the former (with the len call) if None gets stored as a value the code will blow up, whereas the non-len version will work as expected (will eval to false).

Which version is safer and more Pythonic?

Edit: clarifying assumptions: I know the key is in the dictionary, and I know values will be len-able. I also cannot avoid having zero-length values enter the dictionary.

Edit #2: It seems like people are missing the point of my question. I'm not trying to determine the most Pythonic/safest way of checking if a key is present in a dictionary, I'm trying to check if a value has zero length or not

8条回答
老娘就宠你
2楼-- · 2019-02-06 03:44

I'd use a variation of the first option:

>>> mydict = {"key" : "value", "emptykey" : ""}
>>> print bool(mydict["key"])
True
>>> print bool(mydict["emptykey"])
False

Any class that provides __len__ can be converted into a boolean directly (see Truth Value Testing), so bool(container) is the equivalent of bool(len(container)). A length of 0 will become the boolean False while all other lengths will be True. You'll never have a negative length object. Also, the booleans True and False can be printed directly via print, so you don't need the conditional.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-06 03:45

The most Pythonic way would be to not define the undefined value (although whether this is usable depends on what you're using it for) and use in:

mydict = {"key" : "value"}
print "True" if "key" in mydict else "False"  # prints true
print "True" if "emptykey" in mydict else "False"  # prints false

Otherwise, you have three options:

  1. Use mydict.get. You should use this if the key might or might not be in the dictionary.
  2. Use mydict[key]. You should use this if you are certain the key you want is in the dict.
  3. Use len(mydict[key]) > 0. This only works if the value has __len__ defined. Usually, the truth value of a container value depends on the __len__ anyway, so the above are preferable.
查看更多
登录 后发表回答