I'm doing a set difference operation in Python:
from sets import Set
from mongokit import ObjectId
x = [ObjectId("4f7aba8a43f1e51544000006"), ObjectId("4f7abaa043f1e51544000007"), ObjectId("4f7ac02543f1e51a44000001")]
y = [ObjectId("4f7acde943f1e51fb6000003")]
print list(Set(x).difference(Set(y)))
I'm getting:
[ObjectId('4f7abaa043f1e51544000007'), ObjectId('4f7ac02543f1e51a44000001'), ObjectId('4f7aba8a43f1e51544000006')]
I need to get the first element for next operation which is important. How can I retain the list x
in original format?
It looks like you need an ordered set instead of a regular set.
Python doesn't come with an ordered set, but it is easy to make one:
Hope this helps :-)
You could just do this
or
Sets are unordered, so you will need to put the results back in the correct order after doing your set difference. Fortunately you already have the elements in the order you want, so this is easy.
But this can be streamlined; you can do the difference as part of the list comprehension (though it is arguably slightly less clear that that's what you're doing).
You could even do it without creating the
set
fromy
, but theset
will provide fast membership tests, which will save you significant time if either list is large.