0

I have a list of campuses:

campus = [{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'},{'id': '3', 'dlin': '1'},{'id': '4', 'dlin': '2'},{'id': '5', 'dlin': '2'},{'id': '6', 'dlin': '1'}, ]

each campus belongs to a school with a unique dlin. I want to have a list in which I have some other lists, each having a few dictionaries. I run the below code:

schools = []
for i in campus:
    ls = []
    for j in campus:
        if i['dlin'] == j['dlin']:
            ls.append(j)
            # campus_copy.remove(j)
    schools.append(ls)
[print(item) for item in schools]

the result is:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]

I have to either remove the duplicate members from schools or modify the code such that I do not get duplicates. When I try to remove duplicates from schools, I see that dic item is not hashable so I can not do it. To solutions are available that are somewhat similar to my problem. Remove duplicates from list of dictionaries within list of dictionaries Remove duplicate dict in list in Python However, I cannot figure out what to do? does anybody know how to solve the problem?

what I expect to get is:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Amin Ba
  • 1,603
  • 1
  • 13
  • 38

2 Answers2

3

One possible solution is storing the dlin as key in dictionary (and dictionaries cannot have multiple equal keys) rather than removing duplicates explicitly afterwards:

campus = [{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'},{'id': '3', 'dlin': '1'},{'id': '4', 'dlin': '2'},{'id': '5', 'dlin': '2'},{'id': '6', 'dlin': '1'}, ]

schools = {}
for c in campus:
    schools.setdefault(c['dlin'], []).append(c)

for s in schools.values():
    print(s)

Prints:

[{'id': '1', 'dlin': '1'}, {'id': '2', 'dlin': '1'}, {'id': '3', 'dlin': '1'}, {'id': '6', 'dlin': '1'}]
[{'id': '4', 'dlin': '2'}, {'id': '5', 'dlin': '2'}]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • That is what I want but I do not understand what you did. What setdefault does in here – Amin Ba Aug 20 '19 at 13:52
  • @AminBa The setdefault() method returns the value of a key from dictionary (if the key is in dictionary). If the key doesn't exist, it inserts key with a value to the dictionary - in this case empty list `[]` https://docs.python.org/3/library/stdtypes.html#dict.setdefault – Andrej Kesely Aug 20 '19 at 13:54
  • That was great! – Amin Ba Aug 20 '19 at 14:17
0

Based on the answer of Andrej, I solved another part of the question I had and I wanted just to share it here:

My question:

I am now involved in another issue related to the previous one:

I have this list of dictionaries, each informaton of a campus. multiple campuses might belong to a school. I have to distinguish and cluster them based on the similarity of their names.

campus = [
{'id': '1', 'name': 'seneca - york'}, 
{'id': '2', 'name': 'seneca college - north gate campus'},
{'id': '3', 'name': 'humber college - toronto campus'},
{'id': '4', 'name': 'humber college'},
{'id': '5', 'name': 'humber collge - waterloo campus'},
{'id': '6', 'name': 'university of waterloo toronto campus'}, 
]

my expected result can be reached by this small and neat code:

schools = {}
for c in campus:
    schools.setdefault(c['name'][:4], []).append(c)
print(schools)
Amin Ba
  • 1,603
  • 1
  • 13
  • 38