Python – Dictionary

Overview

A Dictionary is an implementation of a data structure which is better known as an associative array. The key difference between a regular array is the index can be a string vs a number. So it is a key value pair that can be stored to look up key and the value associated with it.

dic["key1"] = value1
dic["key2"] = value2
dic["key3"] = value3

Define the Dictionary

Pre-load with static data – Example copied from W3School https://www.w3schools.com/python/python_dictionaries.asp

thisdict = {
  “brand”: “Ford”,
  “model”: “Mustang”,
  “year”: 1964
}
print(thisdict)

Looping through the dictionary

How to create one from a loop of from if statements.

Dictionary variable name and {}. Example valDict = {}

# Globally Defined Dictionay
valDict = {}

def get_column(field, value):
    badValue = "Click HERE to estimate"
    
    if field == "Value A": 
        valDict["ValA"] = value

    if field == "Value B":
        valDict["ValB"] = value


get_column("Value A", "Some Val A") 
get_column("Value B:", "Some Val B") 

print(valDict["QTET"])
print(valDict["QTMPH"])