I've got list of objects. I want to find one (first or whatever) object in this list that has attribute (or method result – whatever) equal to
What's is the best way to find it?
Here's test case:
class Test: def __init__(self, value): self.value = value import random value = 5 test_list = [Test(random.randint(0,100)) for x in range(1000)] # that I would do in Pascal, I don't believe it's anywhere near 'Pythonic' for x in test_list: if x.value == value: print "i found it!" break
I think using generators and
reduce() won't make any difference because it still would be iterating through list.
ps.: Equation to
value is just an example. Of course we want to get element which meets any condition.