Python Insights: How to make use of Dictionaries

Programming or scripting is used to make this attribution into a very usable, visually pleasing product. 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 This email address is being protected from spambots. You need JavaScript enabled to view it.


  

 Use the Buttons to Share this Blog:

Rate this item
(0 votes)

Harmen Romeijn

Harmen joined Spatial Vision as a Senior Geospatial Analyst in 2016.  His principal role is to facilitate in the formulation of modelling and analysis techniques for projects as well as the analysis of geospatial data.  He has strong skills in spatial analysis, modelling and cartography.

Harmen is a skilled analyst and modeller with over 6 years of experience in Geospatial Analysis.  He has a strong background in the ArcGIS environment, Python scripting and the application of automation in modelling processes for large datasets and series.  This has most recently been applied in the evaluation and processing of environmental works program data across Victoria for the analysis of works size and alignment to best management practices. 

For any enquiries please contact us on 1300 36 67 96 or via contact form

Corporate Band

     Mapscape     Check Site     Gis Journey     SV Maps

Login to post comments
Go to top