0

I have the following list of dicts:

defaultdict(<class 'list'>,
            {'192.168.20.10/32': [{1: 'aaaaa11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'}],
             '192.168.20.20/32': [{1: 'aaaaa11111\n'},
                                  {2: 'aaaaa11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'},
                                  {2: 'bbbbb11111\n'}]})

I would like to iterate over this list object and remove the duplicate list items.

The end result would look something like this:

defaultdict(<class 'list'>,
                {'192.168.20.10/32': [{1: 'aaaaa11111\n'},
                                      {2: 'bbbbb11111\n'}],
                 '192.168.20.20/32': [{1: 'aaaaa11111\n'},
                                      {2: 'bbbbb11111\n'}]})

Please note that I am using defaultdict from collections above:

from collections import defaultdict
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
Desmanado
  • 152
  • 9

2 Answers2

2

Maybe something like this (if resulting order does not matter):

adict = {
    '192.168.20.10/32': [
        {1: 'aaaaa11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'}
    ],
    '192.168.20.20/32': [
        {1: 'aaaaa11111\n'},
        {2: 'aaaaa11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'},
        {2: 'bbbbb11111\n'}
    ]
}

for k, v in adict.items():
    adict[k] = list(map(dict, (set([tuple(obj.items()) for obj in v]))))
print(adict)
funnydman
  • 9,083
  • 4
  • 40
  • 55
0

I've used the approach from https://stackoverflow.com/a/11092607/11574713.

Calling your dictionary first_dict, run the dicts through a set by converting them into a string (JSON), and then bring them back to dicts.

import json

for k in first_dict:
    first_dict[k] = [json.loads(j) for j in set([json.dumps(i) for i in first_dict[k]])]
defaultdict(list,
            {'192.168.20.10/32': [{'2': 'bbbbb11111\n'},
              {'1': 'aaaaa11111\n'}],
             '192.168.20.20/32': [{'2': 'aaaaa11111\n'},
              {'2': 'bbbbb11111\n'},
              {'1': 'aaaaa11111\n'}]})
11574713
  • 758
  • 1
  • 4
  • 8