Converting a list to lowercase in Python
Question:
I do not understand why do I get only the last string of the original list? thx
documents = ['Hello, how are you!',
'Win money, win from home.',
'Call me now.',
'Hello, Call hello you tomorrow?']
lc =[]
for x in documents:
lc = x.lower()
print (lc)
Out: hello, call hello you tomorrow?
Answers:
Your code first assigns lc
to an empty array. Then you loop over the original array, and each time through you throw away whatever lc
is assigned to (starting with that unused empty array) and replace it with the lowercase version of that particular string. At the end you leave lc
with the lowercase version of the final string; everything else has been discarded.
What you want to do instead is build a new array from the old one. The pythonic way to do that is with a list comprehension.
lc = [x.lower() for x in documents]
That makes a new array that contains the lowercase version of each element in the original array:
>>> lc
['hello, how are you!', 'win money, win from home.',
'call me now.', 'hello, call hello you tomorrow?']
lc = x.lower()
is re-assigned in each iteration. You need to append this to the empty list lc
.
Way 1: List comprehension
[x.lower() for x in documents]
['hello, how are you!',
'win money, win from home.',
'call me now.',
'hello, call hello you tomorrow?']
for x in documents
: will iterate over all the strings in your list
x.lower()
: will convert to lower case
Way 2: Your code:
lc =[]
for x in documents:
lc.append(x.lower()) # append to the initially empty list
print(lc)
['hello, how are you!', 'win money, win from home.', 'call me now.', 'hello, call hello you tomorrow?']
different approach you can either use it list his
ls = list(map(lambda x : x.lower(), documents))
or map returns you a generator so you can iterate over it as you read the list from documents..can come in handy if you are using very large file. etc..
the reason you are getting this output is becaue of your indentations.
python works with indentations.
you don’t add items to your list, your for loop only assigns each string in your documents to your lc and the last item assigned was ‘Hello, Call hello you tomorrow?’ so then you end your for loop and ask to print whats in the list and theres only 1 item which is converted to lower.
if you write like this:
documents = [‘Hello, how are you!’,
‘Win money, win from home.’,
‘Call me now.’,
‘Hello, Call hello you tomorrow?’]
lc =[]
for x in documents:
lc = x.lower()
print (lc)
it will print out each item as it assigns to a list , but the list is still going to be with just 1 item once the loop is done. to solve that you need to append the list in the for loop
I do not understand why do I get only the last string of the original list? thx
documents = ['Hello, how are you!',
'Win money, win from home.',
'Call me now.',
'Hello, Call hello you tomorrow?']
lc =[]
for x in documents:
lc = x.lower()
print (lc)
Out: hello, call hello you tomorrow?
Your code first assigns lc
to an empty array. Then you loop over the original array, and each time through you throw away whatever lc
is assigned to (starting with that unused empty array) and replace it with the lowercase version of that particular string. At the end you leave lc
with the lowercase version of the final string; everything else has been discarded.
What you want to do instead is build a new array from the old one. The pythonic way to do that is with a list comprehension.
lc = [x.lower() for x in documents]
That makes a new array that contains the lowercase version of each element in the original array:
>>> lc
['hello, how are you!', 'win money, win from home.',
'call me now.', 'hello, call hello you tomorrow?']
lc = x.lower()
is re-assigned in each iteration. You need to append this to the empty list lc
.
Way 1: List comprehension
[x.lower() for x in documents]
['hello, how are you!',
'win money, win from home.',
'call me now.',
'hello, call hello you tomorrow?']
for x in documents
: will iterate over all the strings in your list
x.lower()
: will convert to lower case
Way 2: Your code:
lc =[]
for x in documents:
lc.append(x.lower()) # append to the initially empty list
print(lc)
['hello, how are you!', 'win money, win from home.', 'call me now.', 'hello, call hello you tomorrow?']
different approach you can either use it list his
ls = list(map(lambda x : x.lower(), documents))
or map returns you a generator so you can iterate over it as you read the list from documents..can come in handy if you are using very large file. etc..
the reason you are getting this output is becaue of your indentations.
python works with indentations.
you don’t add items to your list, your for loop only assigns each string in your documents to your lc and the last item assigned was ‘Hello, Call hello you tomorrow?’ so then you end your for loop and ask to print whats in the list and theres only 1 item which is converted to lower.
if you write like this:
documents = [‘Hello, how are you!’,
‘Win money, win from home.’,
‘Call me now.’,
‘Hello, Call hello you tomorrow?’]
lc =[]
for x in documents:
lc = x.lower()
print (lc)
it will print out each item as it assigns to a list , but the list is still going to be with just 1 item once the loop is done. to solve that you need to append the list in the for loop