Select to view content in your preferred language

Removing duplicates from a dictionary

8964
12
Jump to solution
08-26-2016 01:55 PM
JamesCrandall
MVP Frequent Contributor

Hi all!


I'm in need of assistance in removing duplicate values within a single dictionary as I'm not having any success with the ideas/examples I've found.

I need to identify and remove any duplicate "asset" within this dicitionary:

{
 "Features": [
  [
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344E",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344E",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344E",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344E",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344F",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "G344F",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA3A",
     "type": "structure",
     "isActive": 1
    }
   },
   {
    "asset": {
     "isCoastal": 0,
     "name": "S145",
     "WMRegion": "SOUTHERN_REGION",
     "wcuname": "WCA2B",
     "type": "structure",
     "isActive": 1
    }
   }
  ]
 ]
}

0 Kudos
12 Replies
JamesCrandall
MVP Frequent Contributor

Thanks Joshua -- I'm pretty sure I tried to implement that but didn't fully see (as usual) and overlooked what key to use and moved on to the next example.  I will learn one of these days.

I still struggle with dictionaries, but they are really well suited for our GP services output because many of the org's apps are written by JavaScript developers and makes for a slick way to integrate them into our GIS world.  In this instance, the dictionary output is really just the attribute result of spatial selections that run far more efficiently on the ArcGIS Server for obvious reasons, then just compiling the results into a dict to return to the calling application as JSON that they are accustomed to working with.

0 Kudos
ClintonDow1
Occasional Contributor II

Personally I found the listcomp solutions to be a bit hard to grok with the indices (no offense intended though, they are clever!)

This should get you a unique list of 'Asset' namedtuples - doesn't preserve the initial dictionary but since its a list of dictionaries each with a single kvp with a consistent key, its essentially a list anyways  

from collections import namedtuple
asset = namedtuple('Asset', 'isCoastal name WMRegion wcuname type isActive')
unique_assets = []
for (k, v) in d.items():
    for l in v:
        for a in l:
            current = asset(**a['asset'])
            if current not in unique_assets:
                unique_assets.append(current)
#  Asset(isCoastal=0, name='G344E', WMRegion='SOUTHERN_REGION', wcuname='WCA3A', type='structure', isActive=1)
#  Asset(isCoastal=0, name='G344F', WMRegion='SOUTHERN_REGION', wcuname='WCA3A', type='structure', isActive=1)
#  Asset(isCoastal=0, name='S145', WMRegion='SOUTHERN_REGION', wcuname='WCA2B', type='structure', isActive=1)

(Python 3.5 - d.items() would be d.iteritems() in 2.7)

JamesCrandall
MVP Frequent Contributor

Nice!

0 Kudos