Select to view content in your preferred language

Using Python Dictionaries with Update Cursors instead of "Join and Calculate"

3021
1
11-15-2012 10:06 AM
by Anonymous User
Not applicable
As with many others, I was frustrated with the amount of time it takes to use the "Add Join" tool and "Calculate field" tool to get values from one table into another.  To increase the performance, I started playing around with dictionaries and saw that many other people have done this as well to create a dictionary to use with an update cursor so that I do not have to do the "join and calculate" method.  The dictionary method is a very efficient way to update records (since update cursors cannot work with joined tables) and performs this operation about 100x times faster.

I decided to write a couple functions that will use the dictionary and update cursor method to update tables since it is a LOT faster.  I wrote one function that will create a dictionary and update one field in an attribute table based on a dictionary created from another table somewhere else on disk.

Next I created another function that will accept a list of fields to update in the source table, and a list of value fields from another table (join table) that will be used to update the fields in the source table.  It creates a new dictionary for each join table field and updates all input fields in the source table.  The only catch with this is there has to be the same amount of update fields and value fields from both tables.

Anyways, these two functions work great and I am attaching the script that contains them to this post in case anyone is interested.  Also attached is the tool help documentation I hastily put together. They have already saved me a ton of time.  There are two versions, one for Arc 10.0 and one for 10.1.  The only difference is that the 10.1 version uses the da cursors and is faster than the 10.0 version for that reason.  Enjoy!

EDIT: Just took another look at my script and there is one thing I forgot to take out:
                  fieldsDict[field] = join_values[ind]       
This line can be removed.  It won't hurt anything by leaving it in, but if anyone is wondering why it is in there, just ignore that, I forgot to take it out after testing.
0 Kudos
1 Reply
by Anonymous User
Not applicable
Original User: Caleb1987

Oops, I did not attach the correct help document! Here it is! this will better explain things.  Sorry!
0 Kudos