Python 2.7: find item in list ignoring case
Solution 1
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.
Solution 2
You can use List Comprehension to convert the list to lowercase.
if item in [x.lower() for x in stuff]:
print "found"
else:
print "not found"
stuff = ["oranges", "POTATOES", "Pencils", "PAper"]
print [x.lower() for x in stuff]
['oranges', 'potatoes', 'pencils', 'paper']
Solution 3
Convert both strings to either upper case or lower case and compare them?
item = 'paper'
stuff = ["oranges", "POTATOES", "Pencils", "PAper"]
if item.upper() in map(lambda x: x.upper(), stuff):
print "found"
else:
print "Not found"
Extra: Then use this line
if not any ( item.upper() in map(lambda y: y.upper(), x) for x in stuff):
Solution 4
not a python buff and in general new to programming, but well, here is my solution:
I tried to stay close to your general approach, however you might want to look into encapsulating the code within a function.
I don't know about your level of experience, so please pardon if I'm posting something that you already are familiar with
Here some general information on functions: wikipedia
Here Pythons documentation on functions: Python Documentation
First solution, verbose but more understandable for someone new to this:
def item_finder_a(item, stuff):
new_array = []
for w in stuff:
new_array.append(w.lower())
if item in new_array:
print "found"
else:
print "Not found"
item_finder(word,array_of_words)
And the slightly shorter more concise version
def item_finder_b(item, stuff):
if item in map(str.lower,stuff):
print "found"
else:
print "Not found"
item_finder_b(word,array_of_words)
Hope this helps
Cheers
Related videos on Youtube
Comments
-
kasavbere over 1 year
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 printfound
. 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"
-
kasavbere over 11 yearsThanks you so much for the elegant response!