Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the Pythonic way to do it:</p> <pre><code>data = [['a','b'], ['a','c'], ['b','d']] search = 'c' any(e[1] == search for e in data) </code></pre> <p>Or... well, I'm not going to claim this is the "one true Pythonic way" to do it because at some point it becomes a little subjective what is Pythonic and what isn't, or which method is more Pythonic than another. But using <code>any()</code> is definitely more typical Python style than a <code>for</code> loop as in e.g. <a href="https://stackoverflow.com/a/1156114/56541">RichieHindle's answer</a>, </p> <p>Of course there is a hidden loop in the implementation of <code>any</code>, although it breaks out of the loop as soon as it finds a match.</p> <hr> <p>Since I was bored I made a timing script to compare performance of the different suggestions, modifying some of them as necessary to make the API the same. Now, we should bear in mind that fastest is not always best, and being fast is definitely not the same thing as being Pythonic. That being said, the results are... strange. Apparently <code>for</code> loops are very fast, which is not what I expected, so I'd take these with a grain of salt without understanding why they've come out the way they do.</p> <p>Anyway, when I used the list defined in the question with three sublists of two elements each, from fastest to slowest I get these results:</p> <ol> <li><a href="https://stackoverflow.com/a/1156114/56541">RichieHindle's answer</a> with the <code>for</code> loop, clocking in at 0.22 μs</li> <li><a href="https://stackoverflow.com/a/1158136/56541">Terence Honles' first suggestion</a> which creates a list, at 0.36 μs</li> <li><a href="https://stackoverflow.com/a/18412156/56541">Pierre-Luc Bedard's answer (last code block)</a>, at 0.43 μs</li> <li>Essentially tied between <a href="https://stackoverflow.com/a/1156582/56541">Markus's answer</a> and the <code>for</code> loop from <a href="https://stackoverflow.com/q/1156087/56541">the original question</a>, at 0.48 μs</li> <li><a href="https://stackoverflow.com/a/1157153/56541">Coady's answer</a> using <code>operator.itemgetter()</code>, at 0.53 μs</li> <li>Close enough to count as a tie between <a href="https://stackoverflow.com/a/1156628/56541">Alex Martelli's answer</a> with <code>ifilter()</code> and <a href="https://stackoverflow.com/a/1156128/56541">Anon's answer</a>, at 0.67 μs (Alex's is consistently about half a microsecond faster)</li> <li>Another close-enough tie between <a href="https://stackoverflow.com/a/13588121/56541">jojo's answer</a>, mine, <a href="https://stackoverflow.com/a/1156145/56541">Brandon E Taylor's</a> (which is identical to mine), and <a href="https://stackoverflow.com/a/1158136/56541">Terence Honles' second suggestion</a> using <code>any()</code>, all coming in at 0.81-0.82 μs</li> <li>And then <a href="https://stackoverflow.com/a/30449870/56541">user27221's answer</a> using nested list comprehensions, at 0.95 μs</li> </ol> <p>Obviously the actual timings are not meaningful on anyone else's hardware, but the differences between them should give some idea of how close the different methods are.</p> <p>When I use a longer list, things change a bit. I started with the list in the question, with three sublists, and appended another 197 sublists, for a total of 200 sublists each of length two. Using this longer list, here are the results:</p> <ol> <li><a href="https://stackoverflow.com/a/1156114/56541">RichieHindle's answer</a>, at the same 0.22 μs as with the shorter list</li> <li><a href="https://stackoverflow.com/a/1157153/56541">Coady's answer</a> using <code>operator.itemgetter()</code>, again at 0.53 μs</li> <li><a href="https://stackoverflow.com/a/1158136/56541">Terence Honles' first suggestion</a> which creates a list, at 0.36 μs</li> <li>Another virtual tie between <a href="https://stackoverflow.com/a/1156628/56541">Alex Martelli's answer</a> with <code>ifilter()</code> and <a href="https://stackoverflow.com/a/1156128/56541">Anon's answer</a>, at 0.67 μs</li> <li>Again a close-enough tie between my answer, <a href="https://stackoverflow.com/a/1156145/56541">Brandon E Taylor's</a> identical method, and <a href="https://stackoverflow.com/a/1158136/56541">Terence Honles' second suggestion</a> using <code>any()</code>, all coming in at 0.81-0.82 μs</li> </ol> <p>Those are the ones that keep their original timing when the list is extended. The rest, which don't, are</p> <ol start="6"> <li>The <code>for</code> loop from <a href="https://stackoverflow.com/q/1156087/56541">the original question</a>, at 1.24 μs</li> <li><a href="https://stackoverflow.com/a/1158136/56541">Terence Honles' first suggestion</a> which creates a list, at 7.49 μs</li> <li><a href="https://stackoverflow.com/a/18412156/56541">Pierre-Luc Bedard's answer (last code block)</a>, at 8.12 μs</li> <li><a href="https://stackoverflow.com/a/1156582/56541">Markus's answer</a>, at 10.27 μs</li> <li><a href="https://stackoverflow.com/a/13588121/56541">jojo's answer</a>, at 19.87 μs</li> <li>And finally <a href="https://stackoverflow.com/a/30449870/56541">user27221's answer</a> using nested list comprehensions, at 60.59 μs</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload