Python: find object in list 

Joined:
04/09/2007
Posts:
776

November 15, 2009 21:56:19    Last update: November 15, 2009 21:57:59
Python doesn't seem to have a built-in function to find an object in a list that satisfies a specified criterion, for example, making a function f to return True.

To find jack among people:
jack = None
for p in people:
    if p.first_name == 'Jack':
        jack = p
        break


This is not a lot of code, but it's not terse enough as you would expect of Python. Some suggestions are offered here:
  1. http://dev.ionous.net/2009/01/python-find-item-in-list.html
  2. http://tomayko.com/writings/cleanest-python-find-in-list-function
Share |
| Comment  | Tags