Case insensitive 'in' - Python

case insensitive java
case insensitive python
case insensitive javascript
case insensitive regex
case insensitive grep
case-insensitive meaning in tamil
case insensitive meaning in hindi
case insensitive synonym

I love using the expression


where USERNAMES is a list

Is there any way to match items with case insensitivity or do I need to use a custom method? Just wondering if there is need to write extra code for this.

Thanks to everyone!

if 'MICHAEL89' in (name.upper() for name in USERNAMES):


if 'MICHAEL89' in map(str.upper, USERNAMES):

Or, yes, you can make a custom method.

case insensitive, But in case insensitive lowercase uppercase doesn't matter. 2.1k views · View 1 Upvoter. case insensitive. Definition from Wiktionary, the free dictionary. case independent; Translations . treating upper- and lowercase letters as being the same.

I would make a wrapper so you can be non-invasive. Minimally, for example...:

class CaseInsensitively(object):
    def __init__(self, s):
        self.__s = s.lower()
    def __hash__(self):
        return hash(self.__s)
    def __eq__(self, other):
        # ensure proper comparison between instances of this class
           other = other.__s
        except (TypeError, AttributeError):
             other = other.lower()
        return self.__s == other

Now, if CaseInsensitively('MICHAEL89') in whatever: should behave as required (whether the right-hand side is a list, dict, or set). (It may require more effort to achieve similar results for string inclusion, avoid warnings in some cases involving unicode, etc).

Case sensitivity, Examples of case-sensitive and case-insensitive string sorting. These examples show the results of sorts on databases created with various collation and locale  case insensitive (Adjective) Treating or interpreting upper- and lowercase letters as being the same. Often used in computer science to indicate a comparison or equality test that does not distinguish between letters that only differ in case.

Usually (in oop at least) you shape your object to behave the way you want. name in USERNAMES is not case insensitive, so USERNAMES needs to change:

class NameList(object):
    def __init__(self, names):
        self.names = names

    def __contains__(self, name): # implements `in`
        return name.lower() in (n.lower() for n in self.names)

    def add(self, name):

# now this works
usernames = NameList(USERNAMES)
print someone in usernames

The great thing about this is that it opens the path for many improvements, without having to change any code outside the class. For example, you could change the self.names to a set for faster lookups, or compute the (n.lower() for n in self.names) only once and store it on the class and so on ...

What is Case-Sensitive? Webopedia Definition, The module Data.CaseInsensitive provides the CI type constructor which can be parameterised by a string-like type like: String , ByteString  Case-insensitive is when treating or interpreting uppercase and lowercase letters as being the same. Often used in computer science to indicate a comparison or EQUALITY test that does not distinguish between letters that only differ in case.

Here's one way:

if string1.lower() in string2.lower(): 

For this to work, both string1 and string2 objects must be of type string.

What is the difference between case sensitive and case insensitive , Case-insensitive means the program ignores case and matches values regardless of their lower or upper case letters, e.g. “My Name” = “my  Case-Insensitive Search Using UPPER or LOWER Ignoring the case in a where clause is very simple. You can, for example, convert both sides of the comparison to all caps notation: SELECT first_name, last_name, phone_number FROM employees WHERE UPPER(last_name) = UPPER('winand')

I think you have to write some extra code. For example:

if 'MICHAEL89' in map(lambda name: name.upper(), USERNAMES):

In this case we are forming a new list with all entries in USERNAMES converted to upper case and then comparing against this new list.


As @viraptor says, it is even better to use a generator instead of map. See @Nathon's answer.

Examples of case-sensitive and case-insensitive string sorting, username = 'MICHAEL89' if username.upper() in (name.upper() for name in USERNAMES): Alternatively: if username.upper() in  Case-insensitive semantics is supported for type name, property name and function/action name. WebAPI OData will first try to resolve the name with case-sensitive semantics and return the best match if found; otherwise case-insensitive semantics are applied, returning the unique match or throwing an exception if multiple case-insensitive matches exist.

case-insensitive: Case insensitive string comparison, Case-insensitive is when treating or interpreting uppercase and lowercase letters as being the same. Often used in computer science to  Performs a case-insensitive comparison of two objects of the same type and returns a value indicating whether one is less than, equal to, or greater than the other.

Case-insensitive identifiers and emails in Alma, Case insensitive SQL SELECT query FAQ: How do I issue SQL SELECT queries while ignoring case (ignoring whether a string is uppercase or lowercase)?. Background. When I first started writing SQL queries I was using Postgresql, and used some of their custom regular expression capabilities to perform case-insensitive queries.

Case insensitive 'in', That uses the right culture and is case-insensitive, it doesn't allocate temporary lowercase strings, and it avoids the question of whether converting to lowercase and comparing is always the same as a case-insensitive comparison.

  • if 'CaseFudge'.lower() in [x.lower() for x in list]
  • [...] creates the whole list. (name.upper() for name in USERNAMES) would create only a generator and one needed string at a time - massive memory savings if you're doing this operation a lot. (even more savings, if you simply create a list of lowercase usernames that you reuse for checking every time)
  • Prefer to lower all keys when building the dict, for performance reasons.
  • if [x.lower() for x in list] is a list comprehension, is (name.upper() for name in USERNAMES) a tuple comprehension? Or does it have another name?
  • @otocan It's a generator expression.
  • that doesn't work for dict try if CaseInsensitively('MICHAEL89') in {'Michael89':True}:print "found"
  • Xavier: You would need CaseInsensitively('MICHAEL89') in {CaseInsensitively('Michael89'):True} for that to work, which probably doesn't fall under "behave as required".
  • So much for there being only 1 obvious way to do it. This feels heavy unless it's going to be used a lot. That said, it's very smooth.
  • @Nathon, it seems to me that having to invasively alter the container is the "feels heavy" operation. A completely non-invasive wrapper: how much "lighter" than this could one get?! Not much;-). @Xavier, RHS's that are dicts or sets with mixed-case keys/items need their own non-invasive wrappers (part of the short etc. and "require more effort" parts of my answer;-).
  • My definition of heavy involves writing quite a bit of code to make something that will only be used once, where a less robust but much shorter version would do. If this is going to be used more than once, it's perfectly sensible.