Case insensitive matching python
Question:
I want to match items from one list in another without worrying about case sensitivity.
mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
I was doing this before:
for item in mylist2:
if item in mylist1:
print "true"
else:
print "false"
But this fails because it is not case sensitive.
I am aware of re.match(“TeSt”, “Test”, re.IGNORECASE) but how can I apply that to my example?
Answers:
Normalize the case with str.lower()
:
for item in mylist2:
print item.lower() in mylist1
The in
containment operator already returns True
or False
, easiest just to print that:
>>> mylist1 = ['fbh_q1ba8', 'fhh_q1ba9', 'fbh_q1ba10','hoot']
>>> mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
>>> for item in mylist2:
... print item.lower() in mylist1
...
True
False
False
True
False
False
False
If mylist1
contains mixed case values, you’ll need to make the loop explicit; use a generator expression to produce lowercased values; testing against this ensures only as many elements are lowercased as needed to find a match:
for item in mylist2:
print item.lower() in (element.lower() for element in mylist1)
Demo
>>> mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
>>> for item in mylist2:
... print item.lower() in (element.lower() for element in mylist1)
...
True
False
False
True
False
False
False
Another approach is to use any()
:
for item in mylist2:
print any(item.lower() == element.lower() for element in mylist1)
any()
also short-circuits; as soon as a True
value has been found (a matching element is found), the generator expression iteration is stopped early. This does have to lowercase item
each iteration, so is slightly less efficient.
Another demo:
>>> for item in mylist2:
... print any(item.lower() == element.lower() for element in mylist1)
...
True
False
False
True
False
False
False
Why not just do:
for item in mylist2:
if item.lower() in [j.lower() for j in mylist1]:
print "true"
else:
print "false"
This uses .lower()
to make the comparison which gives the desired result.
The other answers are correct. But they dont account for mixed cases in both lists. Just in case you need that:
mylist1 = ['fbh_q1ba8', 'fbh_q1ba9', 'fbh_q1ba10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
for item in mylist2:
found = "false"
for item2 in mylist1:
if item.lower() == item2.lower():
found = "true"
print found
I want to match items from one list in another without worrying about case sensitivity.
mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
I was doing this before:
for item in mylist2:
if item in mylist1:
print "true"
else:
print "false"
But this fails because it is not case sensitive.
I am aware of re.match(“TeSt”, “Test”, re.IGNORECASE) but how can I apply that to my example?
Normalize the case with str.lower()
:
for item in mylist2:
print item.lower() in mylist1
The in
containment operator already returns True
or False
, easiest just to print that:
>>> mylist1 = ['fbh_q1ba8', 'fhh_q1ba9', 'fbh_q1ba10','hoot']
>>> mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
>>> for item in mylist2:
... print item.lower() in mylist1
...
True
False
False
True
False
False
False
If mylist1
contains mixed case values, you’ll need to make the loop explicit; use a generator expression to produce lowercased values; testing against this ensures only as many elements are lowercased as needed to find a match:
for item in mylist2:
print item.lower() in (element.lower() for element in mylist1)
Demo
>>> mylist1 = ['fbH_q1ba8', 'fHh_Q1ba9', 'fbh_q1bA10','hoot']
>>> for item in mylist2:
... print item.lower() in (element.lower() for element in mylist1)
...
True
False
False
True
False
False
False
Another approach is to use any()
:
for item in mylist2:
print any(item.lower() == element.lower() for element in mylist1)
any()
also short-circuits; as soon as a True
value has been found (a matching element is found), the generator expression iteration is stopped early. This does have to lowercase item
each iteration, so is slightly less efficient.
Another demo:
>>> for item in mylist2:
... print any(item.lower() == element.lower() for element in mylist1)
...
True
False
False
True
False
False
False
Why not just do:
for item in mylist2:
if item.lower() in [j.lower() for j in mylist1]:
print "true"
else:
print "false"
This uses .lower()
to make the comparison which gives the desired result.
The other answers are correct. But they dont account for mixed cases in both lists. Just in case you need that:
mylist1 = ['fbh_q1ba8', 'fbh_q1ba9', 'fbh_q1ba10','hoot']
mylist2 = ['FBH_q1ba8', 'trick','FBH_q1ba9', 'FBH_q1ba10','maj','joe','civic']
for item in mylist2:
found = "false"
for item2 in mylist1:
if item.lower() == item2.lower():
found = "true"
print found