Search in lists of lists by given index
the above all look good
but do you want to keep the result?
if so...
you can use the following
result = [element for element in data if element[1] == search]
then a simple
len(result)
lets you know if anything was found (and now you can do stuff with the results)
of course this does not handle elements which are length less than one (which you should be checking unless you know they always are greater than length 1, and in that case should you be using a tuple? (tuples are immutable))
if you know all items are a set length you can also do:
any(second == search for _, second in data)
or for len(data[0]) == 4:
any(second == search for _, second, _, _ in data)
...and I would recommend using
for element in data:
...
instead of
for i in range(len(data)):
...
(for future uses, unless you want to save or use 'i', and just so you know the '0' is not required, you only need use the full syntax if you are starting at a non zero value)
You're always going to have a loop - someone might come along with a clever one-liner that hides the loop within a call to map()
or similar, but it's always going to be there.
My preference would always be to have clean and simple code, unless performance is a major factor.
Here's perhaps a more Pythonic version of your code:
data = [['a','b'], ['a','c'], ['b','d']]
search = 'c'
for sublist in data:
if sublist[1] == search:
print "Found it!", sublist
break
# Prints: Found it! ['a', 'c']
It breaks out of the loop as soon as it finds a match.
(You have a typo, by the way, in ['b''d']
.)
>>> the_list =[ ['a','b'], ['a','c'], ['b''d'] ]
>>> any('c' == x[1] for x in the_list)
True
Here's the Pythonic way to do it:
data = [['a','b'], ['a','c'], ['b','d']]
search = 'c'
any(e[1] == search for e in data)
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 any()
is definitely more typical Python style than a for
loop as in e.g. RichieHindle's answer,
Of course there is a hidden loop in the implementation of any
, although it breaks out of the loop as soon as it finds a match.
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 for
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.
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:
- RichieHindle's answer with the
for
loop, clocking in at 0.22 μs - Terence Honles' first suggestion which creates a list, at 0.36 μs
- Pierre-Luc Bedard's answer (last code block), at 0.43 μs
- Essentially tied between Markus's answer and the
for
loop from the original question, at 0.48 μs - Coady's answer using
operator.itemgetter()
, at 0.53 μs - Close enough to count as a tie between Alex Martelli's answer with
ifilter()
and Anon's answer, at 0.67 μs (Alex's is consistently about half a microsecond faster) - Another close-enough tie between jojo's answer, mine, Brandon E Taylor's (which is identical to mine), and Terence Honles' second suggestion using
any()
, all coming in at 0.81-0.82 μs - And then user27221's answer using nested list comprehensions, at 0.95 μs
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.
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:
- RichieHindle's answer, at the same 0.22 μs as with the shorter list
- Coady's answer using
operator.itemgetter()
, again at 0.53 μs - Terence Honles' first suggestion which creates a list, at 0.36 μs
- Another virtual tie between Alex Martelli's answer with
ifilter()
and Anon's answer, at 0.67 μs - Again a close-enough tie between my answer, Brandon E Taylor's identical method, and Terence Honles' second suggestion using
any()
, all coming in at 0.81-0.82 μs
Those are the ones that keep their original timing when the list is extended. The rest, which don't, are
- The
for
loop from the original question, at 1.24 μs - Terence Honles' first suggestion which creates a list, at 7.49 μs
- Pierre-Luc Bedard's answer (last code block), at 8.12 μs
- Markus's answer, at 10.27 μs
- jojo's answer, at 19.87 μs
- And finally user27221's answer using nested list comprehensions, at 60.59 μs