Aruna Herath
Aruna Herath

Reputation: 6531

Check for duplicates on a repeated property in ndb, appengine

I am using ndb.JsonProperty with repeated = True to store a list of python dicts as an ndb entity. I like the list to contain no duplicates. I thought using something like following would work

    stored_list = TheList(id=list_id)
    current_list = stored_list.list_data
    current_list.extend(items) #items is a list of dicts that need to be newly added

    # check if the list contains duplicate items
    if len(current_list)!=len(set(current_list)):
        cached_list.list_data = current_list
        cached_list.put()

But set(current_list) wouldn't work because dicts are not hashable. I find out some other python solutions to do this but I thought the ndb might contain some feature to prevent repeated properties containing duplicate objects. Doc here contains no such information.

So my question, How to prevent repeated ndb properties containing duplicates?

Upvotes: 1

Views: 736

Answers (1)

Guido van Rossum
Guido van Rossum

Reputation: 16890

There is no NDB feature to support keeping repeated properties free from duplicates. You might want to submit a separate question with the Python tag asking how to do this in general in Python. But it's going to be O(N**2).

Upvotes: 3

Related Questions