Low Orbit Flux Logo 2 F

Python How To Create A Dictionary

This is going to be yet another really quick and simple guide. We are going to show you how to create a dictionary in Python. A dictionary is basically an associative array or a hash table. A dictionary consists of keys and values. A key basically holds a place in the dict. Values can be looked up by their key.

In Python you can create and initialize a dictionary like this:


d = { "Size": 5, "Value": 123, "Color": "blue" }

You can also initialize an empty dictionary like this:


emptyDict = {}

This will also initialize an empty dictionary:


emptyDict = dict()

Every key is unique. If you assign a value to a given key it will overwrite whatever was there.

Setting and overwriting values:


d["Color"] = "green"       # set a value 
d["Color"] = "yellow"      # overwrite