I have a list of two-item lists and need to search for things in it.
If the list is:
list = [['a','b'], ['a','c'], ['b','d']]
I can search for a pair easily by doing
['a','b'] in list
Now, is there a way to see if I have a pair in which a string is present in just the second position? I can do this:
for i in range (0, len(list)): if list[i][1]==search: found=1
But is there a (better) way without the for
loop? I don't need to know i
or keep the loop going after it's found.
any() method return true whenever a particular element is present in a given iterator. # element exists in listof listor not? The 'in' operator is used to check if a value exists in a sequence or not. Evaluates to true if it finds a variable in the specified sequence and false otherwise.
To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.
Using index() method First, iterate through the sublist in the nested list, then check if that particular element exists in that sub_list . If it exists, find the index of the sub_list and the index of the element in that sub_list .
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:
for
loop, clocking in at 0.22 μsfor
loop from the original question, at 0.48 μsoperator.itemgetter()
, at 0.53 μsifilter()
and Anon's answer, at 0.67 μs (Alex's is consistently about half a microsecond faster)any()
, all coming in at 0.81-0.82 μsObviously 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:
operator.itemgetter()
, again at 0.53 μsifilter()
and Anon's answer, at 0.67 μsany()
, all coming in at 0.81-0.82 μsThose are the ones that keep their original timing when the list is extended. The rest, which don't, are
for
loop from the original question, at 1.24 μsIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With