A memo when checking whether the specified key exists in the defined dictionary with python

You can do as follows

test.py


mydict = {
      "item1":"aaa",
      "item2": {
        "item21": "ddd",
      }
}

if "item1" in mydict:
    print("item1 exist!")
else:
    print("item1 not exist!")

if "item2" in mydict:
    print("item2 exist!")

    teststring = "item2"
    testmatrix = mydict[teststring]
    if "item21" in testmatrix:
        print("item21 exist!")
    else:
        print("item21 not exist!")

    if "item22" in testmatrix:
        print("item22 exist!")
    else:
        print("item22 not exist!")

else:
    print("item2 not exist!")

if "item3" in mydict:
    print("item3 exist!")
else:
    print("item3 not exist!")

Run

$ python3 test.py
item1 exist!
item2 exist!
item21 exist!
item22 not exist!
item3 not exist!

reference

Check if the dictionary contains the element of the specified key

Recommended Posts

A memo when checking whether the specified key exists in the defined dictionary with python
[python] How to check if the Key exists in the dictionary
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the value of a specific key up to the specified index in the dictionary list in Python
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
A memo organized by renaming the file names in the folder with python
[Python] Get the files in a folder with Python
A memo when creating a python environment with miniconda
Sort dict in dict (dictionary in dictionary) with a specific key
Execution order when multiple context managers are specified in the Python with statement
A memo that I touched the Datastore with python
A memo of misunderstanding when trying to load the entire self-made module with Python3
When a local variable with the same name as a global variable is defined in the function
Create a dictionary in Python
A memo when face is detected with Python + OpenCV quickly
A memo when creating a directed graph using Graphviz in Python
A story that went missing when I specified a path starting with a tilde (~) in python open
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[Python] Execution time when a function is entered in a dictionary value
How to check the memory size of a dictionary in Python
Read a file in Python with a relative path from the program
A story that didn't work when I tried to log in with the Python requests module
Solve the subset sum problem with a full search in Python
When writing a program in Python
A memo for when pip3 is installed with python2.7 for some reason
[Machine learning pictorial book] A memo when performing the Python exercise at the end of the book while checking the data
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Test & Debug Tips: Create a file of the specified size in Python
A memo corresponding to Django's runserver moss in Python 2.7.11 entered with Homebrew
Receive a list of the results of parallel processing in Python with starmap
Create a record with attachments in KINTONE using the Python requests module
A useful note when using Python for the first time in a while
What is the fastest way to create a reverse dictionary in python?
Behavior when returning in the with block
Precautions when pickling a function in python
Display Python 3 in the browser with MAMP
A memo with Python2.7 and Python3 on CentOS
Check if the URL exists in Python
Run the Python interpreter in a script
Hash in Perl is a dictionary in Python
Dictionary key error → Resolve with key in dicionary
[Python] Read the specified line in the file
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
Use something other than a <br> string for the <br> dict key in Python
[Python] Created a class to play sin waves in the background with pyaudio
What I did when I got stuck in the time limit with lambda python
Compare the sum of each element in two lists with the specified value in Python
Turn multiple lists with a for statement at the same time in Python
Character strings placed in GCS with python are garbled when viewed with a browser
Correspondence memo when the direction key cannot be used on the python command line
How to get a list of files in the same directory with python
Error when installing a module with Python pip
Load the network modeled with Rhinoceros in Python ③
A memo that I wrote a quicksort in Python
Get the caller of a function in Python
[Automation] Extract the table in PDF with Python
Python variadic memorandum when inheriting a defined class
Create a virtual environment with conda in Python
Make a copy of the list in Python
A memo about writing merge sort in Python
Precautions when dealing with control structures in Python 2.6