[PYTHON] How to use any or all to check if it is in a dictionary (Hash)

background

When I want to check if multiple keys and values are in the dictionary type, I thought that any or all could be used, so I will write it as Tips

Overview

For example, check if there are 3 required items and the settings are in the config file

settings = {} # <--The settings are loaded here

#For example, REDIS_HOST and REDIS_PORT and REDIS_When you want to check if there is a key called DB
if not settings['REDIS_HOST'] or not settings['REDIS_PORT'] or not settings['REDIS_DB']:
  print('error')

Don't do it like

requirements = ['REDIS_HOST', 'REDIS_PORT', 'REDIS_DB']
if not all(x in settings for x in requirements):
  print('error')

Can be done like

Recommended Posts

How to use any or all to check if it is in a dictionary (Hash)
[python] How to check if the Key exists in the dictionary
How to check if a value exists in an enum
How to check in Python if one of the elements of a list is in another list
How to check if the contents of the dictionary are the same in Python by hash value
How to use the asterisk (*) in Python. Maybe this is all? ..
How to check the memory size of a dictionary in Python
Hash in Perl is a dictionary in Python
How to use is and == in Python
How to input a character string in Python and output it as it is or in the opposite direction.
How to use dictionary {}
Check if the string is a number in python
A memorandum on how to use keras.preprocessing.image in Keras
Check if it is Unix in the scripting language
python> check NoneType or not> if a == None:> if a is None:
How to use Decorator in Django and how to make it
Check if it is Unix in the scripting language
python> Check if code is printable> Use ord () / all (c in string.printable for c in hello)
How to use the __call__ method in a Python class
How to get all the keys and values in the dictionary
Check "[Windows] How to tell if the exe is x64 or x86 Part2" on MacOS Go
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
[Blender] Script to check if the selected one is a mesh
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Tensorflow-GPU seems to be together if it is TF2.0 or later?
How easy is it to synthesize a drug on the market?
How to use SQLite in Python
How to check the memory size of a variable in Python
Delete a particular character in Python if it is the last
[Introduction to Python] How to use the in operator in a for statement?
How to combine all CSVs in a folder into one CSV
How to use Mysql in python
How to use ChemSpider in Python
Is R's do.call () a classical higher-order function? Learn how to use
I made a function to check if the webhook is received in Lambda for the time being
How to use PubChem in Python
[Ln] How to paste a symbolic link in a directory is complicated
Check if you can connect to a TCP port in Python
How to trick and use a terrible library that is supposed to be kept globally in flask
How to determine if a shell script was started in bash
[Golang] Check if a specific character string is included in the character string
[Python] How to put any number of standard inputs in a list
How to make a container name a subdomain and make it accessible in Docker
Use a shortcut to enable or disable the touchpad in Linux Mint
How to format a list of dictionaries (or instances) well in Python
It is convenient to use Layers when putting a library on Lambda
What is the fastest way to create a reverse dictionary in python?
How to use calculated columns in CASTable
Is it deprecated to use pip directly?
[Pandas] What is set_option [How to use]
How to use Google Test in C
How to get a stacktrace in python
How to check opencv version in python
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use any or all to check if it is in a dictionary (Hash)
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
Check "[Windows] How to tell if the exe is x64 or x86 Part2" on MacOS Go
I made a function to check if the webhook is received in Lambda for the time being
[Blender] Script to check if the selected one is a mesh
Tensorflow-GPU seems to be together if it is TF2.0 or later?
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV