博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
has_key or in
阅读量:4030 次
发布时间:2019-05-24

本文共 3192 字,大约阅读时间需要 10 分钟。

393
51

I wonder what is better to do:

d = {
'a': 1, 'b': 2}'a' in dTrue

or:

d = {
'a': 1, 'b': 2}d.has_key('a')True
 

11 Answers

546
accepted

in is definitely more pythonic.

In fact .

 
2  
As an addition, in Python 3, to check for the existence in values, instead of the keys, try >>> 1 in d.values() –   
76  
One semi-gotcha to avoid though is to make sure you do: "key in some_dict" rather than "key in some_dict.keys()". Both are equivalent semantically, but performance-wise the latter is much slower (O(n) vs O(1)). I've seen people do the "in dict.keys()" thinking it's more explicit & therefore better. –   
1  
in works with 2.6 too right? –   
 
@Logan yes it does –   
1  
@BenjaminSchollnick what's the result? –   
179

in wins hands-down, not just in elegance (and not being deprecated;-) but also in performance, e.g.:

$ python -mtimeit -s'd=dict.fromkeys(range(99))' '12 in d'10000000 loops, best of 3: 0.0983 usec per loop$ python -mtimeit -s'd=dict.fromkeys(range(99))' 'd.has_key(12)'1000000 loops, best of 3: 0.21 usec per loop

While the following observation is not always true, you'll notice that usually, in Python, the faster solution is more elegant and Pythonic; that's why -mtimeit is SO helpful -- it's not just about saving a hundred nanoseconds here and there!-)

 
2  
thanks a lot, good to know, now I'm changing my code to use 'in' instead of has_key() ;) –   
3  
Thanks for this, made verifying that "in some_dict" is in fact O(1) much easier (try increasing the 99 to say 1999, and you'll find the runtime is about the same). –   
 
has_key appears to be O(1) too. –   
43

According to python :

has_key() is deprecated in favor of key in d.

 
 
thanks a lot, it really helped! –   
28

Use dict.has_key() if (and only if) your code is required to be runnable by Python versions earlier than 2.3 (when key in dict was introduced). 

 
6  
While one would hope no one is using a Python earlier than 2.3 (released in 2003), I'm quite confident there are still some holdouts. So this answer is a valuable footnote to all the "use in of course, duh" answers. –   
2  
@JohnY This really comes into play with the embedded linux variants. I'm currently stuck using 2.3 on two projects :( –   
1  
The WebSphere update in 2013 uses Jython 2.1 as its main scripting language. So this is unfortunately still a useful thing to note, five years after you noted it. –   
13

There is one example where in actually kills your performance.

If you use in on a O(1) container that only implements __getitem__ and has_key() but not __contains__ you will turn an O(1) search into an O(N) search (as in falls back to a linear search via __getitem__).

Fix is obviously trivial:

def __contains__(self, x):    return self.has_key(x)
 
8

has_key is a dictionary method, but in will work on any collection, and even when __contains__ is missing, in will use any other method to iterate the collection to find out.

转载地址:http://fvhbi.baihongyu.com/

你可能感兴趣的文章
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
电平触发方式和边沿触发的区别
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Spring事务的七种传播行为
查看>>