I am finding the fastest way to search item in a list. As I found that in Python, it is better to use set
to search item instead of using a list
. So I replaced the list
by set
. However, all items in the set
are an object. I want to search if there's object id equals to the id that I want to find. If it is, then return that object.
I can do it in a simple for-loop, but I do not know how can it be improved in set if I still loop all elements to find the item.
def find(allItems, id):
for item in allItems:
if (item.getId() == id):
return item
from sets import Set
allItems = Set()
allItems.add(itemObj)
find(allItems, 1)