We have created the Python Insights blog series to help other Python users by providing solutions to common problems and handy hints.
The final instalment in the series will help explain how to make use of dictionaries in Python. A dictionary in python is analogous to a dictionary in real-life. It is a collection of key reference words that is used to lookup words, numbers or other entries.
Use of Dictionaries in Python
Unlike lists or arrays in python, a dictionary is a non-ordered collection of entries. Each entry does not have a designated spot within the dictionary. A set of values in the dictionary is linked to its reference, not unlike how in an actual dictionary a definition is linked to a word.
A dictionary in python retains the order in which entries were placed into it, but when using a dictionary it can be read in any order by calling upon the key word.
To create a dictionary in python you use the curly brackets – {} – to establish an empty dictionary. This can be seen in line 5 of the example in Figure 5. A dictionary principally has the form of {key word: definition}.
In this example we want to create a dictionary of road route numbers by using road names as the reference key word. Using a SearchCursor, we can iterate through the table of our selected shapefile and start to populate our empty dictionary. This is done from line 8 where we specify “Name” at row[0] to be our key word and “Route_Num” at row[1] to be our definition.
These 4 lines of text can be refined into one line by employing some list comprehension, as shown on line 10.
Figure 1: Dictionary Example
We can now utilise this compiled dictionary of road names and route numbers to update other shapefiles. You can create a dictionary off any field in a table and put multiple definitions to a key value.
In the sample in Figure 2, we have noticed that some of the route numbers in the parent Australia road layer are incorrect. We can use the dictionary to update this layer. From line 15 we specify a key value, which is the road names of the Australia road layer. We use this key word to look through our dictionary. The statement from line 16 specifies that if the name matches the key word in the dictionary then we need to update the route number with that associated with the key word in the dictionary.
Figure 2: Combining Cursors and Dictionaries
The use of dictionaries and cursors can be a great pythonic way of performing spatial joins and updates to shape files and database tables without the need to go into an editing session in ArcMap.
We hope you have enjoyed our Python Insights series and have learnt some skills that you can apply in your own workplace. Hopefully this will provide more confidence when writing your python code in ArcGIS, please let us know how you go using our contacts details below.
For more information on python in ArcGIS, please make reference to the ArcGIS desktop user’s manual, online at (http://desktop.arcgis.com/en/arcmap/latest/analyze/python/what-is-python-.htm), or ask us at Spatial Vision how we can assist your teams in how python coding in ArcGIS can improve up your production workflows and other advance spatial analytics using GIS.
For further information, please contact Spatial Vision at info@spatialvision.com.au
- When to Raster vs When to Vector? A worked example - May 4, 2022
- Mapping the Future of Farming - August 9, 2019
- Great Ocean Road Erosion Impacts - January 10, 2019