Introduction to Python Dictionaries
In this chapter, we will explore Python dictionaries, which are collections of key-value pairs. The dictionary is Python's implementation of the map.
For example, consider the following map:
| Key | Value |
|---|---|
| US | United States |
| UK | United Kingdom |
This dictionary contains 2 entries. The entry key is the country code, and the entry value is the corresponding country name.
Here is the same map as a Python dictionary:
{ "US": "United States", "UK": "United Kingdom"}
A dictionary is a set of entries surrounded by curly braces ( { } ). An entry is a key and a value separated by a colon (:). As usual, Python ignores whitespace characters unless they are part of a string.
Here is a dictionary with 1 entry:
{ "Name": "Alex" }
A dictionary can span multiple lines. Here is an example with 3 entries:
{ 1: "one",
2: "two",
3: "three" }
The entries are seprated by commas.
Both keys and values are expressions. Here is another valid dictionary:
{ 1 + 2: "three",
add(1, 1): subtract(3, 1),
"Bob": "Dole" }
If we write a dictionary with duplicate keys, Python will only keep the last entry that contains the key.
For instance, if you wrote the following dictionary:
{ 1: "one",
2: "two",
1: "three" }
Python will store the following in the Object Store:
{ 1: "three",
2: "two" }
Finally, dictionaries are expressions, so we can assign them to variables. For example:
x = {
"US": "USA",
"UK": "United Kingdom"
}
Retrieving a Dictionary Entry Value
As with lists, we can use square brackets to access dictionary entries. But while we accessed list elements by numerical index, we access dictionary entries by the entry key. For example:
x["US"] == "USA"
Python looks for the entry with key "US". The output is the entry value "USA".
The dictionary index is an expression. For example, we can use variables as indices:
name = "UK" x[name] # "United Kingdom"
Also, we can assign the get item operation to a variable. For example:
y = x[name]
Set Dictionary Entry
As with lists, we can update a dictionary entry using the set item operation. Here is an example:
x["UK"] = "France"
This updates the entry with the key "UK" to value "France".
If an entry with the key does not exist, a new entry will be created. For example,
x["NG"] = "Nigeria"
creates a new entry with key "NG" and value "Nigeria".
Retrieving Dictionary Keys
We can call the keys method to retrieve the dictionary entry keys. Here is an example program:
x = {
"AW": "Aruba",
"CU": "Cuba",
"FJ": "Fiji"
}
keys = x.keys()
len(keys) == 3 # True
keys is [ "AW", "CU", "FJ" ]. We can use the keys() method to:
- Calculate the number of entries in the dictionary.
- Determine whether the dictionary contains some key.
Note: In Python 3, the keys method returns a view rather than a list, but it still serves the same purpose.
Dictionaries and the Object Store
As with Python lists, LW Python stores dictionaries in the Object Store. For example, consider the following assignment:
y = { 'name': 'John' }
Python pushes the dictionary to the Object Store, then creates a new Variables Map entry with key y and value reference, where reference is the location of the dictionary object in the Object Store.
Try running the example program below to get a feel for how dictionaries work.
| Current Line | 1 | Current Tab | 0 | Time | 0 |
Write a Python expression that returns the value for key "KR" in a dictionary called country_names below.
Write a Python expression that updates the value for key "KR" in a dictionary called country_names to "Korea"
When LW Python executes code such as:
x = { 'name': 'Jane' }
Where does LW Python store the dictionary?
Comments
Please log in to add comments