Use Python To Create A Dictionary With Keys And Values

The python dictionary is similar to an associative array but an array uses a numeric index. The Dictionary is an unordered key-value pair and the values can be any type of Python data. In our example, we create a Dictionary and unique keys to assign values.

Simple Dictionary Example

We will use some Car manufacturing data to explain the basics of working with a Dictionary in Python. Create a dictionary by using the curly brackets and assigning a Key and Value to each. The we can PRINT the values by referencing the key.

dict1 =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

## get all the keys
print(dict1.keys()) 

## use the keys to get the values
print(dict1["brand"], dict1["model"], dict1["year"])  

In the course of writing your application the “value” in our new Dictionary may need to be changed. Here we see that we can just reference the key and use the equal ( = ) sign to assign a new value.

dict1["year"] = 1999

print(dict1["year"])

Dictionary With a List As The Value

A better solution is to use a Python “List” so we can have more than one “value” for each “key”. Lets convert the value for our “Year” key to a list by using brackets [] and reassign the previous year. Then we can append more years to this value later.

dict1["year"] = [1964]

print(dict1["year"])

Append Value To Existing Key

We can now append more values to the year “List” by simply using the plus-equal ( += ) sign, in this example we are appending just the numbers and not a string (“”). In our example wee can now identify multiple years for this manufacturer and model.

dict1["year"] += [1965,1966,1967]

print(dict1["year"])

Get The Value by Key In A Dictionary | get()

We can use the Dictionary Get method to pull the value out of the Dictionary for one key.

## Get all of the keys

print(dict1.keys())

## Get a single value

print(dict1.get("band")

## Get the "list" of Years, as a list

print(dict1.get("year")

Get All Values & Keys From A Dictionary Separately

More useful methods include the values() and keys(), which returns just the “keys” or “values” respectively. If your following along with the above example then we can return a single value for the “brand” and “model” along with a List for the “year” as well as just the keys.

print(dict1.values())

print(dict1.keys())

Append A Key Value Pair | update()

We can also update the existing key-value pairs or simply add another entry to our dictionary as long as the key is unique. If we use an existing key then the value will be updated instead of a new one added.
Let’s change the brand and model to something different.

dict1.update({"brand": "Chevy"})

dict1.update({"model": "Suburban"})

print(dict1["brand"], dict1["model"], dict1["year"])

Now we are missing a value for the Color and the Mileage of the vehicle so we can add a new item with keys called “color” and “mileage”.

dict1.update({"color": "Green"})

dict1.update({"mileage": 89000})

print(dict1["brand"], dict1["model"], dict1["year"], dict1["color"], dict1["mileage"])