Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting a list of strings to lowercase breaks down the words into characters

I have a list of sentences like so:
['Business World - user unable to log in', 'Cannot Login', 'customer getting no signal message on projector', 'Account - colleague is unable to access account', 'Password reset after interruption'] of about 8000 elements.

I want to convert the above list to lowercase. I tried using the following line:

my_list = [x.lower() for x in my_list]

But that gives the error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

AttributeError: ‘int’ object has no attribute ‘lower’

So I did the following to ensure that all elements are considered as strings:

my_list = [x.lower() for x in str(my_list)]

This returns the lowers case, but breaks down each word into individual characters?

['[',
 "'",
 'b',
 'u',
 's',
 'i',
 'n',
 'e',
 's',
 's',
 ' ',
 'w',
 'o',
 'r',
 'l',
 'd',
 ' ',
 '-',

What am I doing wrong?

>Solution :

str(my_list) serializes your list object to a single string object. Iterating over the string yields every single character in this serialization.

What you probably want is to convert each element into a string (apparently there are also numbers in it outside of your sample) and lowercase that.

lower_list = [str(x).lower() for x in my_list]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading