139

Let's assume I'm creating a simple class to work similar to a C-style struct, to just hold data elements. I'm trying to figure out how to search a list of objects for objects with an attribute equaling a certain value. Below is a trivial example to illustrate what I'm trying to do.

For instance:

class Data:
    pass

myList = []

for i in range(20):
    data = Data()
    data.n = i
    data.n_squared = i * i
    myList.append(data)

How would I go about searching the myList list to determine if it contains an element with n == 5?

I've been Googling and searching the Python docs, and I think I might be able to do this with a list comprehension, but I'm not sure. I might add that I'm having to use Python 2.4.3 by the way, so any new gee-whiz 2.6 or 3.x features aren't available to me.

1
  • Perhaps an unintentional quirk of your example: myList = [Data().n==0, Data().n=1, ...] where data.n would be assigned by range() and data.n would be the index into myList. Therefore allowing you to pull up any Data() instance just by referencing myList by an index value. Of course you could later modifiy myList[0].n = 5.2 or something. And the example was perhaps over-simplified.
    – DevPlayer
    Dec 22, 2016 at 15:48

11 Answers 11

210

You can get a list of all matching elements with a list comprehension:

[x for x in myList if x.n == 30]  # list of all elements with .n==30

If you simply want to determine if the list contains any element that matches and do it (relatively) efficiently, you can do

def contains(list, filter):
    for x in list:
        if filter(x):
            return True
    return False

if contains(myList, lambda x: x.n == 3)  # True if any element has .n==3
    # do stuff
3
  • 30
    or, any(custom_filter(x) for x in myList if x.n == 30) which is just your "contains" function as a builtin.
    – nosklo
    Feb 28, 2009 at 20:12
  • Syntax error on nosklo -- need an extra set of () around the generator.
    – gahooa
    Feb 28, 2009 at 20:16
  • 1
    it would be good to merge this answer together with gahooa's (stackoverflow.com/a/598602/2349267). Jan 13, 2014 at 8:59
98

Simple, Elegant, and Powerful:

A generator expression in conjuction with a builtin… (python 2.5+)

any(x for x in mylist if x.n == 10)

Uses the Python any() builtin, which is defined as follows:

any(iterable) -> Return True if any element of the iterable is true. Equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
3
  • Nice. FYI you can do any(x for x in mylist if x.n == 10) to save some parens (also == not =). Feb 28, 2009 at 23:57
  • I prefer use any(x for x in mylist if x['n'] == 10) but is good idea Oct 5, 2018 at 22:06
  • 1
    Very classy solution. Improves readability too imo.
    – thiras
    Nov 23, 2020 at 2:50
59
[x for x in myList if x.n == 30]               # list of all matches
[x.n_squared for x in myList if x.n == 30]     # property of matches
any(x.n == 30 for x in myList)                 # if there is any matches
[i for i,x in enumerate(myList) if x.n == 30]  # indices of all matches

def first(iterable, default=None):
  for item in iterable:
    return item
  return default

first(x for x in myList if x.n == 30)          # the first match, if any
2
  • 2
    This is a good answer because of the "first" method, which is probably the most common use case.
    – galarant
    Jan 28, 2013 at 22:01
  • great, thanks! the match indices was what I was looking for. Is there a shortcut to use this to directly index the list to access another field? Now I get a list of list entries (there is just one entry, so it's a list with one item). To get the index, I need to perform result[0] before I can use it to index the list. From the question example, I want to access n_squared from a particular n: myList[index of myList.n==5].n_squared
    – Frieke
    Mar 22, 2019 at 14:16
58

Just for completeness, let's not forget the Simplest Thing That Could Possibly Work:

for i in list:
  if i.n == 5:
     # do something with it
     print "YAY! Found one!"
0
51
filter(lambda x: x.n == 5, myList)
3
  • 28
    for someone who wants to learn Python, understanding lambda is basic.
    – vartec
    Mar 1, 2009 at 19:02
  • 3
    Well, yes and no -- with list comprehensions and sorting key functions makers like operator.attrgetter, I hardly ever use lambdas.
    – Ben Hoyt
    Mar 5, 2012 at 15:42
  • I like simple 👍
    – gdibble
    Dec 19, 2021 at 8:32
13

Another way you could do it is using the next() function.

matched_obj = next(x for x in list if x.n == 10)
10

You can use in to look for an item in a collection, and a list comprehension to extract the field you are interested in. This (works for lists, sets, tuples, and anything that defines __contains__ or __getitem__).

if 5 in [data.n for data in myList]:
    print "Found it"

See also:

5

You should add a __eq__ and a __hash__ method to your Data class, it could check if the __dict__ attributes are equal (same properties) and then if their values are equal, too.

If you did that, you can use

test = Data()
test.n = 5

found = test in myList

The in keyword checks if test is in myList.

If you only want to a a n property in Data you could use:

class Data(object):
    __slots__ = ['n']
    def __init__(self, n):
        self.n = n
    def __eq__(self, other):
        if not isinstance(other, Data):
            return False
        if self.n != other.n:
            return False
        return True
    def __hash__(self):
        return self.n

    myList = [ Data(1), Data(2), Data(3) ]
    Data(2) in myList  #==> True
    Data(5) in myList  #==> False
3

Consider using a dictionary:

myDict = {}

for i in range(20):
    myDict[i] = i * i

print(5 in myDict)
2
  • Or: d = dict((i, i*i) for i in range(20))
    – hughdbrown
    Mar 1, 2009 at 22:08
  • It solves the trivial problem I used to illustrate my question, but didn't really solve my root question. The answer I was looking for (5+ years ago) was the list comprehension. :)
    – m0j0
    Jul 18, 2014 at 19:16
1

Use the following list comprehension in combination with the index method:

data_n = 30
j = [data.n for data in mylist].index(data_n)
print(mylist[j].data.n == data_n)
1
  • What does this do if the value's not found?
    – parsley72
    Nov 23, 2023 at 19:51
0

I'm surprised there's no answer that asserts that only one item matches.

Maybe this does that:

def one_matching(list, test):
  filtered = [item for item in list if test(item)]
  if len(filtered) != 1:
    if len(filtered) == 0:
      raise KeyError("No matching value found.")
    else:
      raise KeyError("Multiple matching values found.", *filtered)
  return filtered[0] 

# prints 2:
print(one_matching([1,2,3], lambda x: x**2 == 4))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.