Home > Enterprise >  Remove all but the first occurrence of a value in a nested Python dictionary
Remove all but the first occurrence of a value in a nested Python dictionary

Time:09-21

I have the following dictionary:

d1 = {'test': ['1', '2', '3'],
      'cool': ['7', '1'],
      'idk': ['3', '2', '7', '1'],
      'idc': ['8'],
      'hm': ['1', '4', '5']}

The output I'm trying to achieve is:

{'test': ['1', '2', '3'],
 'cool': ['7'],
 'idk': [''],
 'idc': ['8'],
 'hm': ['4','5']}

Here's the code i've tried:

result = {}

for key, value in d1.items():
    if value not in result.values():
        result[key] = value

print("result", str(result))

This isn't giving me the output i'm expecting. I'm trying to keep the first value and remove any occurrences of that value after that. Any help would be greatly appreciated.

CodePudding user response:

This should do what you need:-

d1 = {'test': ['1', '2', '3'],
      'cool': ['7', '1'],
      'idk': ['3', '2', '7', '1'],
      'idc': ['8'],
      'hm': ['1', '4', '5']}

kv = []
d2 = {}
for k, v in d1.items():
    ta=[]
    for n in v:
        if not n in kv:
            kv.append(n)
            ta.append(n)
    d2[k]=ta
print(d2)

CodePudding user response:

You can create a set variable that will contain all seen values so far:

d1 = {
    "test": ["1", "2", "3"],
    "cool": ["7", "1"],
    "idk": ["3", "2", "7", "1"],
    "idc": ["8"],
    "hm": ["1", "4", "5"],
}

out, seen = {}, set()
for k, v in d1.items():
    tmp = []
    for vv in v:
        if vv not in seen:
            tmp.append(vv)
            seen.add(vv)
    out[k] = tmp or [""]
print(out)

Prints:

{
    "test": ["1", "2", "3"],
    "cool": ["7"],
    "idk": [""],
    "idc": ["8"],
    "hm": ["4", "5"],
}

Alternative without set:

out = {}
for k, v in d1.items():
    for vv in v:
        if not any(vv == vvv for l in out.values() for vvv in l):
            out.setdefault(k, []).append(vv)
    out[k] = out.get(k, [""])
print(out)
  • Related