<p>I don't remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,</p>

<pre><code>foo in iter_attr(array of python objects, attribute name)</code></pre>

<p>I've looked over the docs but this kind of thing doesn't fall under any obvious listed headers</p>

[{'answer_id': 701, 'author': 'saalon', 'author_id': 111, 'author_profile': 'https://Stackoverflow.com/users/111', 'pm_score': 3, 'selected': False, 'text': '<p>No, you were not dreaming.  Python has a pretty excellent list comprehension system that lets you manipulate lists pretty elegantly, and depending on exactly what you want to accomplish, this can be done a couple of ways.  In essence, what you\'re doing is saying "For item in list if criteria.matches", and from that you can just iterate through the results or dump the results into a new list.</p>\n\n<p>I\'m going to crib an example from <a href="http://diveintopython.net/functional_programming/filtering_lists.html" rel="noreferrer">Dive Into Python</a> here, because it\'s pretty elegant and they\'re smarter than I am.  Here they\'re getting a list of files in a directory, then filtering the list for all files that match a regular expression criteria.</p>\n\n<blockquote>\n<pre><code>    files = os.listdir(path)                               \n    test = re.compile("test\\.py$", re.IGNORECASE)          \n    files = [f for f in files if test.search(f)]\n</code></pre>\n</blockquote>\n\n<p>You could do this without regular expressions, for your example, for anything where your expression at the end returns true for a match.  There are other options like using the filter() function, but if I were going to choose, I\'d go with this.</p>\n\n<p>Eric Sipple</p>\n'}, {'answer_id': 735, 'author': 'akdom', 'author_id': 145, 'author_profile': 'https://Stackoverflow.com/users/145', 'pm_score': 0, 'selected': False, 'text': '<p>I think:</p>\n\n<pre><code>#!/bin/python\nbar in dict(Foo)\n</code></pre>\n\n<p>Is what you are thinking of.  When trying to see if a certain key exists within a dictionary in python (python\'s version of a hash table) there are two ways to check.  First is the <strong><code>has_key()</code></strong> method attached to the dictionary and second is the example given above.  It will return a boolean value.</p>\n\n<p>That should answer your question.</p>\n\n<p>And now a little off topic to tie this in to the <em>list comprehension</em> answer previously given (for a bit more clarity).  <em>List Comprehensions</em> construct a list from a basic <em>for loop</em> with modifiers.  As an example (to clarify slightly), a way to use the <code>in dict</code> language construct in a <em>list comprehension</em>:</p>\n\n<p>Say you have a two dimensional dictionary <strong><code>foo</code></strong> and you only want the second dimension dictionaries which contain the key <strong><code>bar</code></strong>.  A relatively straightforward way to do so would be to use a <em>list comprehension</em> with a conditional as follows:</p>\n\n<pre><code>#!/bin/python\nbaz = dict([(key, value) for key, value in foo if bar in value])\n</code></pre>\n\n<p>Note the <strong><code>if bar in value</code></strong> at the end of the statement**, this is a modifying clause which tells the <em>list comprehension</em> to only keep those key-value pairs which meet the conditional.**  In this case <strong><code>baz</code></strong> is a new dictionary which contains only the dictionaries from foo which contain bar (Hopefully I didn\'t miss anything in that code example... you may have to take a look at the list comprehension documentation found in <a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" rel="nofollow noreferrer">docs.python.org tutorials</a> and at <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow noreferrer">secnetix.de</a>, both sites are good references if you have questions in the future.).</p>\n'}, {'answer_id': 745, 'author': 'Matt', 'author_id': 154, 'author_profile': 'https://Stackoverflow.com/users/154', 'pm_score': 4, 'selected': False, 'text': '<p>Are you looking to get a list of objects that have a certain attribute? If so, a <a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" rel="noreferrer">list comprehension</a> is the right way to do this.</p>\n\n<pre><code>result = [obj for obj in listOfObjs if hasattr(obj, \'attributeName\')]\n</code></pre>\n'}, {'answer_id': 750, 'author': 'Brendan', 'author_id': 199, 'author_profile': 'https://Stackoverflow.com/users/199', 'pm_score': 3, 'selected': False, 'text': "<p>What I was thinking of can be achieved using list comprehensions, but I thought that there was a function that did this in a slightly neater way.</p>\n\n<p>i.e. 'bar' is a list of objects, all of which have the attribute 'id'</p>\n\n<p>The mythical functional way:</p>\n\n<pre><code>foo = 12\nfoo in iter&#95;attr(bar, 'id')</code></pre>\n\n<p>The list comprehension way:</p>\n\n<pre><code>foo = 12\nfoo in [obj.id for obj in bar]</code></pre>\n\n<p>In retrospect the list comprehension way is pretty neat anyway.</p>\n"}, {'answer_id': 31126, 'author': 'dwestbrook', 'author_id': 3119, 'author_profile': 'https://Stackoverflow.com/users/3119', 'pm_score': 3, 'selected': False, 'text': '<p>you could always write one yourself:</p>\n\n<pre><code>def iterattr(iterator, attributename):\n    for obj in iterator:\n        yield getattr(obj, attributename)\n</code></pre>\n\n<p>will work with anything that iterates, be it a tuple, list, or whatever.</p>\n\n<p>I love python, it makes stuff like this very simple and no more of a hassle than neccessary, and in use stuff like this is hugely elegant.</p>\n'}, {'answer_id': 31188, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 2, 'selected': False, 'text': "<p>If you plan on searching anything of remotely decent size, your best bet is going to be to use a dictionary or a set.  Otherwise, you basically have to iterate through every element of the iterator until you get to the one you want.</p>\n\n<p>If this isn't necessarily performance sensitive code, then the list comprehension way should work.  But note that it is fairly inefficient because it goes over every element of the iterator and then goes BACK over it again until it finds what it wants.</p>\n\n<p>Remember, python has one of the most efficient hashing algorithms around.  Use it to your advantage.</p>\n"}, {'answer_id': 57833, 'author': 'Will Harris', 'author_id': 4702, 'author_profile': 'https://Stackoverflow.com/users/4702', 'pm_score': 7, 'selected': True, 'text': "<p>Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before <code>in</code> could start its search.</p>\n\n<p>The temporary list can be avoiding by using a generator expression:</p>\n\n<pre><code>foo = 12\nfoo in (obj.id for obj in bar)\n</code></pre>\n\n<p>Now, as long as <code>obj.id == 12</code> near the start of <code>bar</code>, the search will be fast, even if <code>bar</code> is infinitely long.</p>\n\n<p>As @Matt suggested, it's a good idea to use <code>hasattr</code> if any of the objects in <code>bar</code> can be missing an <code>id</code> attribute:</p>\n\n<pre><code>foo = 12\nfoo in (obj.id for obj in bar if hasattr(obj, 'id'))\n</code></pre>\n"}, {'answer_id': 4905822, 'author': 'shang', 'author_id': 572606, 'author_profile': 'https://Stackoverflow.com/users/572606', 'pm_score': 3, 'selected': False, 'text': '<p>The function you are thinking of is probably <code>operator.attrgettter</code>. For example, to get a list that contains the value of each object\'s "id" attribute:</p>\n\n<pre><code>import operator\nids = map(operator.attrgetter("id"), bar)</code></pre>\n\n<p>If you want to check whether the list contains an object with an id == 12, then a neat and efficient (i.e. doesn\'t iterate the whole list unnecessarily) way to do it is:</p>\n\n<pre><code>any(obj.id == 12 for obj in bar)</code></pre>\n\n<p>If you want to use \'in\' with attrgetter, while still retaining lazy iteration of the list:</p>\n\n<p><pre><code>import operator,itertools\nfoo = 12\nfoo in itertools.imap(operator.attrgetter("id"), bar)\n</pre></code></p>\n'}]
['https://Stackoverflow.com/questions/683', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/199/']