Python 2.7: find item in list ignoring case
python, python-2.7
Solution
I'd combine `lower` with `any`:
>>> stuff = ["oranges", "POTATOES", "Pencils", "PAper"]
>>> any(s.lower() == 'paper' for s in stuff)
True
>>> any(s.lower() == 'paperclip' for s in stuff)
False
This will short-circuit and stop searching as soon as it finds one (unlike a listcomp). OTOH, if you're going to be doing multiple searches, then you might as well use a listcomp to lower the whole list once.
For your updated case (why is it that no one ever asks the question they're interested in, but a different question instead?), I'd probably do something like
>>> any("book" in (s.lower() for s in x) for x in stuff)
True
>>> any("paper" in (s.lower() for s in x) for x in stuff)
True
>>> any("stuff" in (s.lower() for s in x) for x in stuff)
False
The same rule holds, though. If you're doing multiple searches, you're probably better off canonicalizing the list-of-lists once.
Problem
I have a list of strings ``` ["oranges", "POTATOES", "Pencils", "PAper"] ``` I want to find whether the list contains `paper`, ignoring case; so that the following code snippet should print `found`. My list only contains simple strings constituted of only the English alphabet -- upper and lower cases. ``` item = 'paper' stuff = ["oranges", "POTATOES", "Pencils", "PAper"] if item in stuff: print "found" else: print "Not found" #How do I get the method to print "found"? ``` CLARIFICATION: My list is actually a list of lists and my logic is using the following construct: ``` if not any ( item in x for x in stuff): print "Not found" else: print "found" ```