Dictionaries in python are similar to “associative arrays” in some other languages. Dictionaries are indexed by keys, which can be any immutable type, e.g strings and numbers.

Creating a dictionary

#Create a doctionary
my_dictionary = {}
my_dictionary["MyKeyA"] = "Something"
my_dictionary["MyKeyB"] = "Something else"

#Alternative method

my_dictionary = {"MyKeyA": "Something", "MyKeyB": "Something else"}

Accessing a dictionary

print(my_dictionary["MyKeyB"])

Does dictionary key exist?

    if 'MyKeyName' in my_dictionary:
        print("MyKeyName: " + my_dictionary['MyKeyName'])
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *