Dictionaries, also called dicts, are Python’s native mapping data type. Dictionaries allow rapid data lookup and enumerations; both very useful in using Python for programming and data analysis.
Dictionaries are unordered and are defined by key:value
pairs in curly brackets { }
and typcially hold data that are related such as information contained in a user profile. In our example, since I work with geologic data, I will use some hypothetical exploration drillhole information.
A dictionary looks like this:
DDH2018_001 = {'northing':1506890, 'easting':4569873, 'elevation':6456, 'depth':1465,
'surveyed':True,'surveyor':'IDS'}
Keys & Values
For your reference DDH2018_001
is a drillhole ID and the keys
are northing
, easting
, and elevation
(geographical coordinates), depth,Surveyed
, and surveyor
.
The values
in this example are 1506890
, 4569873
, 6456
, 1465
, True
, and 'IDS'
. Values
can be any datatype. In this example there are integer, Boolean, and string data.
Printing
print(DDH2018_001)
Accessing Values with Keys
Values in dictionaries are easy to access by calling the key of the data you need. To see just the depth of DDH20018_001 we do the following:
print(DDH2018_001['depth'])
Dictionaries are similar to databases in that it is possible to retrieve data by calling a key, which is acting like record number.
print(DDH2018_001['surveyed'])
print(DDH2018_001['surveyor'])
Accessing Dictionary Elements with Methods
It is also possible to use built-in dictionary methods to obtain information about a dictionary:
dict.keys()
– returns all keys
dict.values()
– returns all values
dict.items()
– returns all items in a list of
(key,value)
. – tuple pairs
print(DDH2018_001.keys())
This returned an iterable list of dict keys.
print(DDH2018_001.values())
This returned an iterable list of dict values.
print(DDH2018_001.items())
This returned an iterable list of (key,value)
pairs.
These methods provide iterable view objects for the dict.keys()
, dict.values()
, dict.items()
classes. This makes it possible to query across dicts to find common values and differences. We will compare DDH2018_001
and DDH2018_002
:
DDH2018_001 = {'northing':1506890, 'easting':4569873, 'elevation':6456,
'depth':1465,'surveyed':True,'surveyor':'IDS'}
DDH2018_002 = {'northing':150000, 'easting':4569450, 'elevation':6502,
'depth':1503,'surveyed':True,'surveyor':'MineDept'}
for common_key in DDH2018_001.keys() & DDH2018_002.keys():
print(DDH2018_001[common_key], DDH2018_002[common_key])
From compairing these two drillholes we can see they have the same keys, or fields, and all fields are contain data. This can be extremely useful when comparing large dictionaries.
Another trick we can do is iterate the view of dict.items
from above and incorporte them into a sentence using a for loop. This can be useful when extracting data into a readable format for human consumption.
for key, value in DDH2018_001.items():
print(key, 'is the key for the value', value)
Modifying Dictionaries
Since dictionaries are mutable, it is possible to make changes to them by adding, deleting, and changing elements.
To add a value we execute dict[key]=value
.
proposed_holeID= {'P1':'DDH2018_001', 'P2':'DDH2018_002'}
proposed_holeID['P3'] = 'DDH2018_003'
print(proposed_holeID)
The dictionary of proposed drillhole IDs has been updated to include P3 : DDH2018_003
. Using this same syntax it is possible to change any value in a dictionary. Let’s assume that the survey for DDH2018_002
needs to be removed from the dictionary because the readings were flawed. To show that DDH2018_002
has been removed, we will change the dictionary to show survey:False
DDH2018_002['surveyed']=False
print(DDH2018_002)
It also possible to modify dictionaries using the dict.update()
. Since the the surveyed
value for DDH2018_002
has been changed to False
we need to update surveyor
to None
.
DDH2018_002.update({'surveyor':'None'})
print(DDH2018_002)
We can see that 'surveyor'
record has been successfully updated to 'survyeor':'None'
.
Deleting Elements from a Dictionary
Deleting elements from a dicitonary is just as easy as adding and updating dictionary elements. To delete a dictionary element we will use del dict[key]
. Let’s imagine we discovered all of the elevation data for DDH2018_002 is wrong and we just want to delete until the correct coordinates can be provided. To delete the coordinates we perform the following:
print(DDH2018_002)
del DDH2018_002['elevation']
print(DDH2018_002)
The command del DDH2018_002['elevation']
has removed the elevation data as demonstrated by the two lists above. We have now learned that all of the information for DDH2018_002
is incorrect so we will clear all values by executing dict.clear()
.
print(DDH2018_002)
DDH2018_002.clear()
print(DDH2018_002)
As we can see from the empty brackets that were returned when printing DDH2018_002
the information was successfully cleared. Now, since there is not need for DDH2018_002
it can be deleted completely like so:
del DDH2018_002
print(DDH2018_002)
Since DDH2018_002
was deleted, we receive an error when we try to print it.
Final Thoughts
In this overview we covered some of the most commonly used dictionary functions and methods. There are many more you will pick up as you learn more Python and I will show more advanced dictionary methods in a future post if you would like to come back and learn more. If this has been useful to you, or if you have some good dictionary please leave a comment below or contact me on Twitter.