Python Forum
lower() applied at in operator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: lower() applied at in operator (/thread-25160.html)



lower() applied at in operator - Beerforfree - Mar-22-2020

Is there a way to use lower() or casefold() at the "in" operator like
if 'string' in these_list.lower()

I know this is not possible, but there should be a way to achieve this in an elegant way


RE: lower() applied at in operator - Larz60+ - Mar-22-2020

You can use like:
if val.lower() in this_list:
but not the way you try. lower must be applied to individual item, therefore list doesn't qualify.


RE: lower() applied at in operator - Beerforfree - Mar-22-2020

I've the problem on the list or dictionary side which I read from a file as csv, josn, etc


RE: lower() applied at in operator - ndc85430 - Mar-22-2020

As Larz60+ told you, you can't call lower on a list (or a dictionary) for that matter. It doesn't make sense does it? Turning something to lowercase only makes sense if that something is a string. A list of strings is not a string! If you want to do that to all the items in a list, for example, either write a for loop, or a list comprehension to produce a new list.