Get the difference between two lists as a feature selection

That title is a bit convoluted, but it sort of summarises what I was trying to do. I had two similar datasets, and had to return the features in the first dataset which were not in the second dataset. Spatially, this could be achieved by using the Erase tool. I used this tool often during my internship, when I created many ad hoc maps and had to create masks for various study areas.

I needed to extract a text-based list though, so I turned to set difference in Python.

drak = [row[0] for row in arcpy.da.SearchCursor("DSG", "SG21Code")]
sga = [row[0] for row in arcpy.da.SearchCursor("SGA", "SGA.ID")]
lr_diff = lambda l, r: list(set(l).difference(r))
rest = lr_diff(sga, drak)
qry = """ SGA.ID IN (\'""" + "\', \'".join(rest) + "\')""SGA","NEW_SELECTION", qry)

In Lines 1 and 2, I extract all values in the field for my left and right datasets (sga and drak respectively). Those are funny names for variables, but I see that I copied and pasted my code into the gist as is, instead of changing the variable names like I normally do. I was probably under pressure (as always).

In Line 3 I define a lambda function. Now, I tend to forget these exist, especially when using ArcPy. I also am not entirely sure of when and why to use it. Nevertheless, I put one here to get the set difference between my left and right datasets i.e. all the values which only existed in sga.

In Line 6 I build the SQL query for the where_clause for the Select Layer by Attribute tool. I’ve been trying to use more SQL syntax as one tends to forget that many things are possible in ArcGIS.

I remember vividly an exercise from a GIS module in 3rd year, where instead of writing “TOWN IN (A,B,C,D)”, we were taught to write “TOWN = A OR TOWN = B OR TOWN = C OR TOWN = D”. At the time the GIS course was heavily skewed to the social sciences as those students made up the majority of the class. We were instructed to do things a certain way. It contributed to why I felt (at the time), that GIS was too easy for me, and I would rather focus on Remote Sensing. I’m really glad that I could find out for myself that things didn’t have to be that way.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.