Home > database >  Delete duplicate dictionary form a list of dictionaries
Delete duplicate dictionary form a list of dictionaries

Time:11-27

I want to find duplicate directories from a list of dictionaries and delete one of them but it's generating an error. name, age, group only all 3 should be same values to take it as duplicate dictionary

a = [
  {"name": "Tom", "age": 21,"group":"sdd","points":0},
  {"name": "Mark", "age": 5,"group":"sdo","points":0},
  {"name": "Pam", "age": 7,"group":"spp","points":0},
  {"name": "Tom", "age": 21,"group":"sdd","points":0},
  {"name": "Buke", "age": 31,"group":"pool","points":0}
]

print(a)
for i in range(len(a)):
  for j in range(i 1,len(a)):
    if a[i] == a[j]:
      a.pop[j]
      

print(a)

CodePudding user response:

You can convert each dict to a tuple, then apply set to remove duplicates. At the end back to the list of dicts.

a = [
  {"name": "Tom", "age": 21,"group":"sdd","points":0},
  {"name": "Mark", "age": 5,"group":"sdo","points":0},
  {"name": "Pam", "age": 7,"group":"spp","points":0},
  {"name": "Tom", "age": 21,"group":"sdd","points":0},
  {"name": "Buke", "age": 31,"group":"pool","points":0}
]

a_new = list(map(dict, set(tuple(dct.items()) for dct in a)))
print(a_new)

Output:

[{'name': 'Mark', 'age': 5, 'group': 'sdo', 'points': 0},
 {'name': 'Tom', 'age': 21, 'group': 'sdd', 'points': 0},
 {'name': 'Buke', 'age': 31, 'group': 'pool', 'points': 0},
 {'name': 'Pam', 'age': 7, 'group': 'spp', 'points': 0}]

CodePudding user response:

If I understand you correctly, the duplicate is only when name, age and group match:

a = [
    {"name": "Tom", "age": 21, "group": "sdd", "points": 0},
    {"name": "Mark", "age": 5, "group": "sdo", "points": 0},
    {"name": "Pam", "age": 7, "group": "spp", "points": 0},
    {"name": "Tom", "age": 21, "group": "sdd", "points": 0},
    {"name": "Buke", "age": 31, "group": "pool", "points": 0},
]

out, seen = [], set()
for d in a:
    tpl = d["name"], d["age"], d["group"]
    if tpl not in seen:
        seen.add(tpl)
        out.append(d)

print(out)

Prints:

[
    {"name": "Tom", "age": 21, "group": "sdd", "points": 0},
    {"name": "Mark", "age": 5, "group": "sdo", "points": 0},
    {"name": "Pam", "age": 7, "group": "spp", "points": 0},
    {"name": "Buke", "age": 31, "group": "pool", "points": 0},
]
  • Related