Home > front end >  Removing duplicates from a list in a nested dictionary using setdefault()
Removing duplicates from a list in a nested dictionary using setdefault()

Time:06-10

I'm creating a nested dictionary where the inner dictionaries value part will be a list that should maintain order. I can achieve a solution but I would like to do this when I first create the dictionary(dict1) instead of using another dictionary(dict2) to achieve this? I have omitted some code for simplicity and possible confusion since I'm using ignition designer which uses jython as its interpreter. The idea for the most part should be self explanatory.

example: {20:{Mon Apr 04 00:00:00 CDT 2022: [90, 90, 1, 1, 2, 3, 4, 5]}} = 

         {20:{Mon Apr 04 00:00:00 CDT 2022: [90, 1, 2, 3, 4, 5]}}
dict1, dict2 = defaultdict(dict), defaultdict(dict)

# Nested dictionary with duplicates and order.
for i in dataset1:
    for j dataset2:
       dict1[i[0]].setdefault(j[0], []).append(round(j[1], 2))

# Nested dictionary with no duplicates and no order.
for i in dataset1:
    for j dataset2:
        dict1[i[0]].setdefault(j[0], set()).add(round(j[1], 2))

# Here I'm able to remove the duplicates and keep order but would like to do this
# when I'm first creating the dictionary (dict1)?
dict2 = {i:{j:sorted(set(k), key=k.index) for j, k in dict1[i].items()} for i in dict1}

CodePudding user response:

dict or - traditionally - collections.OrderedDict preserve insertion order:

from collections import OrderedDict, defaultdict

class OrderedSet(OrderedDict):
    def add(self, x):
        self[x] = None

>>> dict1 = defaultdict(lambda: defaultdict(OrderedSet))
>>> dict1[1][2].add(4)
>>> dict1[1][2].add(3)
>>> dict1[1][2].add(5)
>>> dict1[1][2].add(4)
>>> list(dict1[1][2])
[4, 3, 5]

CodePudding user response:

Since you have tagged Python-2.7 you can use OrderedDict to get ordered set as dict does not preserve order.

from collections import OrderedDict
def ordered_set(values):
    d = OrderedDict()
    for v in values:
        d[v] = None
    return list(d.keys())

dict1 = {k: {k2: ordered_set(v2) for k2, v2 in dict1[k].items()} for k,v in dict1.items()}
  • Related