Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Last active December 31, 2015 15:59
Show Gist options
  • Save stavxyz/8010570 to your computer and use it in GitHub Desktop.
Save stavxyz/8010570 to your computer and use it in GitHub Desktop.
python -- unique list

Determines whether a list nested in mongodb already contains the value.
If not, write it.

def is_a_new_thing(an_id):
    doc = DB.collection.get('stuff')
    the_ids = doc['ids']
    setbefore = set(the_ids)
    setafter = set(the_ids).add(an_id)
    # adding an existing value to a set will not change the set
    if setafter.difference(setbefore):
        doc.update({'ids': list(the_ids.add(an_id))})
        DB.collection.save('stuff', doc)
        return True
    else:
        return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment