Search code examples
pythoncase-sensitive

How to avoid case sensitivity in Python


I have this code and want to compare two lists.

list2= [('Tom','100'),('Alex','200')]
list3= [('tom','100'),('alex','200')]

non_match = []
for line in list2:
    if line not in list3:
       non_match.append(line)

print(non_match)

The results will be:

[('Tom', '100'), ('Alex', '200')]

because of case sensitivity! is there any way to avoid the case sensitivity in this case? I don't want to change the lists to upper or lower case.

Or any other method which can match these lists?


Solution

  • This worked for me! Both lists will be converted to lower case.

    list2= [('Tom','100'),('Alex','200'),('Tom', '13285')]
    list3= [('tom','100'),('ALex','200'),('Tom', '13285')]
    
    
     def make_canonical(line):
         name, number = line
         return (name.lower(), number)
    
     list22 = []
     for line2 in list2:
        search = make_canonical(line2)
        list22.append(search)
    
    
     list33 =[]
     for line3 in list3:
         search1 = make_canonical(line3)
         list33.append(search1)
    
    
     non_match = []
     for line in list22:
         if line not in list33:
            non_match.append(line)
    
    
     print(non_match)