etayluz
etayluz

Reputation: 16416

Python: Make list of dictionaries unique with respect to one key

I have a list of dictionaries where one of the key values is NOT unique:

arr = [{'host': '144.217.103.15', 'port': 3000}, 
       {'host': '158.69.115.201', 'port': 8080},
       {'host': '144.217.103.15', 'port': 1020},]

I want to make the given array unique in regards to the 'host' key so that the final output would be:

result = [{'host': '158.69.115.201', 'port': 8080}, 
          {'host': '144.217.103.15', 'port': 1020},]

or it could be:

result = [{'host': '144.217.103.15', 'port': 3000}, 
          {'host': '158.69.115.201', 'port': 8080},]

What is the Pythonic way of doing so?

Upvotes: 2

Views: 853

Answers (2)

Eric Duminil
Eric Duminil

Reputation: 54223

You could just convert to a dict and extract the values:

>>> { d['host']: d for d in arr }.values()
[{'host': '144.217.103.15', 'port': 1020}, {'host': '158.69.115.201', 'port': 8080}]

For Python3, you could convert the dict_values to a list:

>>> list({d['host']: d for d in arr}.values())
[{'host': '144.217.103.15', 'port': 1020}, {'host': '158.69.115.201', 'port': 8080}]

If you want to keep the original order (minus the host duplicates), you could use an OrderedDict:

>>> from collections import OrderedDict
>>> list(OrderedDict( (d['host'], d) for d in arr).values())
[{'host': '144.217.103.15', 'port': 1020}, {'host': '158.69.115.201', 'port': 8080}]

Finally, if you want a list of dictionary with unique host and port, you could use a tuple as key:

>>> list(OrderedDict(((d['host'], d['port']), d) for d in arr).values())
[{'host': '144.217.103.15', 'port': 3000}, {'host': '158.69.115.201', 'port': 8080}, {'host': '144.217.103.15', 'port': 1020}]

Upvotes: 8

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

Keeping the first entry:

arr = [{'host': '144.217.103.15', 'port': 3000}, 
       {'host': '158.69.115.201', 'port': 8080},
       {'host': '144.217.103.15', 'port': 1020},]
hosts = set()
out = []
for entry in arr:
    if not entry['host'] in hosts:
        out.append(entry)
        hosts.add(entry['host'])
print(out)

#[{'host': '144.217.103.15', 'port': 3000}, {'host': '158.69.115.201', 'port': 8080}]

Upvotes: 1

Related Questions