Searching a List of Objects for those Objects with a Particular Member Variable set to a Value?
Question: 
    import string
    import random
    
    class Foo(object):
        def __init__(self, bar):
            self.bar = bar
    
    foos = []
    for i in range(1,101):
        f = foo(random.choice(string.letters))
        foos.append(f)
    
    fs == find_object_in_list_by_attribute(bar='b')
    

Is there a method like `find_object_in_list_by_attribute` in python to
accomplish this?


Answer: Just use the `[, if <filter>]` grammar of list comprehensions to give you the
foos with `bar == 'b'`:

    
    
    fs = [foo for foo in foos if foo.bar == 'b']
    

sidenotes: classes should start with capital letters (like ruby, but just
_strongly recommended_ instead of compulsory) and range objects are written as
`for i in range(100):`.

In the event that you just want the _first_ foo (which may or may not be the
only foo), you can do:

    
    
    fs = next(foo for foo in foos if foo.bar == 'b')
    

This will notably raise a `StopIteration` exception if `'b'` is not found
anywhere in your `foos` collection, so you can give `next` a "fallback" value
to avoid that:

    
    
    fs = next((foo for foo in foos if foo.bar == 'b'), None)
    

