开发者

How to test if a dictionary contains a specific key? [duplicate]

开发者 https://www.devze.com 2023-02-17 11:13 出处:网络
This question already has answers here: Check if a given key already exists in a d开发者_Go百科ictionary
This question already has answers here: Check if a given key already exists in a d开发者_Go百科ictionary (16 answers) Closed 3 years ago.

What's the cleanest way to test if a dictionary contains a key?

x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
    ....


'a' in x

and a quick search reveals some nice information about it: http://docs.python.org/3/tutorial/datastructures.html#dictionaries

0

精彩评论

暂无评论...
验证码 换一张
取 消