--- title: "Dictionaries" --- # Dictionaries > A Dictionary is a mapping object. Where sequences are indexed with a numeric index value, dictionaries are indexed using keys. These keys then have a matching value pair that is associated with a particular key. For example, with a list we can extract the object at index 0, which we may have decided is the name, whereas with dictionaries, I can instead extract the a value using a key "name". Because of how they work, dictionaries are sometimes known as associative arrays in other programming languages. ## Mapping Type Dictionaries are created using braces `{` `}` , where each key/value pair is separated by a comma ( , ) and keys are separated from their values using a colon ( : ). In the example below, I created a dictionary with two keys: name, id. ```python title="Python - Creating a Dictionary" # In this dictionary, I associated the name John Smith to the key "name", # and the id number 12345 to the key "id". myDictionary = {"name":"John Smith", "id":12345} ``` ### Using a Dictionary The keys in a dictionary can be numbers, strings, or tuples, but typically a string is used to make a key that best describes the value. Any given key may only appear once in a dictionary, so trying to set another value for a key that already exists will overwrite the previous value for that key. Alternately, attempting to access the value of a key that does not exist will throw an error, while setting a value to a key that does not exist will create a new key/value pair within the dictionary. To access a value in a dictionary works much like accessing a value in a list; simply place brackets containing the key after the dictionary object. ```python title="Python - Accessing Values in a Dictionary" # Creates a dictionary with three key/value pairs. myDictionary = {'Bob': 89.9, 'Joe': 188.72, 'Sally': 21.44} print myDictionary['Joe'] # Will print out: 188.72 # Adds a key for 'Amir', and alters the value associated with the key 'Sally'. myDictionary['Amir'] = 45.89 myDictionary['Sally'] = 146.23 print myDictionary # Will print out the whole dictionary: {'Joe': 188.72, 'Amir': 45.89, 'Bob': 89.9, 'Sally': 146.23} ``` It is also easy to loop through all of the values of a dictionary using the keys() function. For example: ```python title="Python - Keys Function" # The keys() function provides us with a list of keys, which we can iterate through and print out in addition to using in the value lookup. for key in myDict.keys(): print key, myDict[key] ``` There are many use cases for dictionaries, but they are commonly used in Ignition when passing values into a Message Handler or [creating a dynamic roster](ignition-modules/alarm-notification/alarm-notification-pipelines/pipeline-blocks/notification-block.md#calculated) for alarms. ## Dictionary Functions Dictionaries have a few functions that allow for greater control over the dictionary object and the values contained within. |Function |Description |Example |Output | |--|--|--|--| |len(dictionary)|Returns the number of items in the dictionary.|
myDictionary = `{"name":"John Smith", "id":12345}`print len (myDictionary)
|
2
| |del dictionary[key] |Will remove the named key. |
myDictionary = `{"name":"John Smith", "id":12345}`del myDictionary["id"]print myDictionary 
|
`{'name': 'John Smith'}`
| |key in dictionary |Will return True if the dictionary has that key.Can also use "key not in dictionary" |
myDictionary = `{"name":"John Smith", "id":12345}`if "name" in myDictionary:    print myDictionary["name"]
|
John Smith
| |dictionary.clear() |Remove all of the items in the dictionary. |
myDictionary = `{"name":"John Smith", "id":12345}`myDictionary.clear()print myDictionary
|
`{}` 
| |dictionary.keys() |Returns a list of the dictionary's keys. |
myDictionary = `{"name":"John Smith", "id":12345}`print myDictionary.keys()
|
['name', 'id'] 
| |dictionary.values() |Returns a list of the dictionary's values. |
myDictionary = `{"name":"John Smith", "id":12345}`print myDictionary.values() 
|
['John Smith', 12345]
|