-2

I have a list of dictionaries, for example:

list_of_clients = [
    {"Name": "Alex", "Surname": "Dorfman", ...},
    {"Name": "Serge", "Surname": "Ioffe"}, ...
]

and I know that there are duplicate dictionaries in the list for the same client.

Is there any way to print them out quickly?

Thanks!

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
Павел Иванов
  • 1,863
  • 5
  • 28
  • 51

2 Answers2

1

You could keep a set of already seen names. You have to define which keys should be considered. In this case : ["Name", "Surname"]. Simply add more if you want:

list_of_clients = [{"Name": "Alex", "Surname": "Dorfman"}, {"Name": "Serge", "Surname": "Ioffe"}, {"Name": "Alex", "Surname": "Dorfman"}, {"Name": "Serge", "Surname": "Ioffe"}]

already_seen = set()

for client in list_of_clients:
    complete_name = tuple(client.get(k) for k in ["Name", "Surname"])
    if complete_name in already_seen:
        print("Duplicate : %s" % (complete_name,))
    already_seen.add(complete_name)

It outputs:

Duplicate : ('Alex', 'Dorfman')
Duplicate : ('Serge', 'Ioffe')
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 2
    Problem being, I don't think the Name and Surname are the only keys... `{"Name": "Alex", "Surname": "Dorfman", ...}` (OP's example, note the ellipsis) – cs95 Jun 26 '17 at 11:59
  • @Coldspeed Also note that the whole syntax was incorrect. My method is flexible. If there are more interesting keys, simply add them. – Eric Duminil Jun 26 '17 at 12:08
0

You can try:

res = set(tuple(client.items() 
                for client in list_of_clients 
                if list_of_clients.count(client) > 1))

The set() will get rid of duplicates in the result, and the tuple() is required (at least in Python3) because the items() themselves are still not hashable.

As others mentioned this will not keep the items in order, but if you just want to remove the duplicates this is sufficient.

mwil.me
  • 1,134
  • 1
  • 19
  • 33
  • It would be better to use `if list_of_clients.count(client) > 1 and client not in list_of_clients`, such that the duplicates only appear once in the list, and not each time they occur. (Not my downvote though) – Mr. Xcoder Jun 26 '17 at 12:02
  • @Mr.Xcoder `client` is always in `list_of_clients`, it's better to use a set then. – mwil.me Jun 26 '17 at 12:08