I would use list comprehensions. Suppose this is your Client
class:
>>> class Client:
... def __init__(self, screenName):
... self.screenName = screenName
If I got this list of clients:
>>> l = [Client('a'), Client('b'), Client('c')]
...I can get a list containing only the clients with a given name:
>>> [e for e in l if e.screenName == 'b']
[<__main__.Client instance at 0x2e52b0>]
Now, just get the first - and assumedly only - element:
>>> [e for e in l if e.screenName == 'b'][0]
<__main__.Client instance at 0x2e52b0>
>>> c = [e for e in l if e.screenName == 'b'][0]
>>> c.screenName
'b'
This is pretty short and IMHO elegant but can be less efficient because the list comprehension will iterate over all the list. If you do want to avoid this overhead, you can get an generator instead of a new list using parenthesis instead of square brackets:
>>> g = (e for e in l if e.screenName == 'b')
>>> g
<generator object <genexpr> at 0x2e5440>
>>> g.next()
<__main__.Client instance at 0x2e52b0>
However, note that the next()
method can be called just once.
HTH!