[python] How to check if the Key exists in the dictionary

single key Use in to check if the Key exists in the Dictionary.

Either key in dict.keys () or key in dict can be used as shown below.

In [2]: D = {'abc': 1, 'def': 2, 'ghi': 3, 'jkl' : 4}

In [3]: 'abc' in D.keys()
Out[3]: True

In [4]: 'xyz' in D.keys()
Out[4]: False

In [5]: 'abc' in D
Out[5]: True

By the way, if there is a value, you should use in as well.

In [6]: 1 in D.values()
Out[6]: True

In [7]: 5 in D.values()
Out[7]: False

multiple keys

How to check if all keys exist

Use Dictionary view object to check if multiple keys exist. The method of getting the Dictonary view object has changed between python2.7 and python3. Use viewkeys () for python2.7 and keys () for python3.

python2.7

python2.7


In [8]: D.viewkeys() >= {'abc', 'def'}
Out[8]: True

In [9]: D.viewkeys() >= {'abc', 'xyz'}
Out[9]: False

python3 In python3, viewkeys becomes keys.

In [3]: D.keys() >= {'abc', 'def'}
Out[3]: True

In [4]: D.keys() >= {'abc', 'xyz'}
Out[4]: False

To make it work on both, use six and write as follows.

In [6]: import six

In [7]: six.viewkeys(D) >= {'abc', 'def'}
Out[7]: True

In [8]: six.viewkeys(D) >= {'abc', 'xyz'}
Out[8]: False

How to use set

The method that @shiracamus told me in the comments. In this case, there is no need to import six and it is neat.

In [21]: set(D) >= {'abc', 'def'}
Out[21]: True

In [22]: set(D) >= {'abc', 'xyz'}
Out[22]: False

How to use for loop & all

The method that @shiracamus told me in the comments.

In [2]: all(key in D for key in ('abc', 'def'))
Out[2]: True

In [3]: all(key in D for key in ('abc', 'xyz'))
Out[3]: False

How to check if at least one key exists

In dictionary view object, you can use & to take intersection. https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects

python2.7

In [14]: len(D.viewkeys() & {'abc', 'xyz'}) > 0
Out[14]: True

In [15]: len(D.viewkeys() & {'x', 'xyz'}) > 0
Out[15]: False

python3

In [10]: len(D.keys() & {'abc','xyz'}) > 0
Out[10]: True

In [11]: len(D.keys() & {'x','xyz'}) > 0
Out[11]: False

How to use for loop & any

The method that @shiracamus told me in the comments.

In [25]: any(key in D for key in ('abc', 'xyz'))
Out[25]: True

In [26]: any(key in D for key in ('abc', 'def'))
Out[26]: True

In [27]: any(key in D for key in ('x', 'xyz'))
Out[27]: False

reference: https://stackoverflow.com/questions/16004593/python-what-is-best-way-to-check-multiple-keys-exists-in-a-dictionary https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects

Recommended Posts

[python] How to check if the Key exists in the dictionary
Check if the URL exists in Python
How to check the memory size of a dictionary in Python
How to check if the contents of the dictionary are the same in Python by hash value
How to check if a value exists in an enum
How to check opencv version in python
How to check in Python if one of the elements of a list is in another list
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
How to use the C library in Python
Check if the characters are similar in Python
How to get the files in the [Python] folder
How to develop in Python
Check if the string is a number in python
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to get the number of digits in Python
Check if the expected column exists in Pandas DataFrame
How to know the current directory in Python in Blender
How to use the exists clause in Django's queryset
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
How to use the model learned in Lobe in Python
[Python] How to output the list values in order
How to automatically check if the code you wrote in Google Colaboratory corresponds to the python coding standard "pep8"
[Python] How to do PCA in Python
How to debug the Python standard library in Visual Studio
How to collect images in Python
How to store Python function in Value of dictionary (dict) and call function according to Key
How to use the __call__ method in a Python class
How to use SQLite in Python
[Python] How to write an if statement in one sentence.
In the python command python points to python3.8
How to get the Python version
How to use any or all to check if it is in a dictionary (Hash)
A memo when checking whether the specified key exists in the defined dictionary with python
[Python] How to import the library
How to get the last (last) value in a list in Python
How to use Mysql in python
How to get all the keys and values in the dictionary
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
How to turn the for statement when there are multiple values for one key in the dictionary
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
Check if the password hash generated by PHP matches in Python
The 15th offline real-time how to write reference problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
Difference in how to write if statement between ruby ​​and python
[TensorFlow 2] How to check the contents of Tensor in graph mode
Check if you can connect to a TCP port in Python
The 14th offline real-time how to write reference problem in python
Have python check if the string can be converted / converted to int
[Beginner memo] How to specify the library reading path in Python
The 18th offline real-time how to write reference problem in Python
How to display bytes in the same way in Java and Python
[Introduction to Python] How to use class in Python?