#๐Ÿ”’ why .lower() no work?

40 messages ยท Page 1 of 1 (latest)

buoyant mesa
#

why does this not make all the items in the list lowercase? it still print the list with the items in uppercase

cloud phoenixBOT
#

@buoyant mesa

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dense niche
#

You want to make another list that has the lower case items, and then append the items from the upper case list

brittle pasture
buoyant mesa
#

why it dont change names[0] tho and only item>

#

?

brittle pasture
#

I think thats just how variables in python work.

#
i=0
for item in names:
   names[i]=item.lower();i+=1

just make sure you are making change to names[i] and not in item

wary lance
#

when you use .lower() is creates a new copy of the string and lowers all letters instead of changing the data in-place

#

so you willl have to explicitly replace the old string with the lowered one in the list

buoyant mesa
#

alr i see

#

thanks guys

idle cedar
#

!e

original_name = 'ALEX'
name = original_name
name = name.lower()

print(f'{original_name=}, {name=}')
cloud phoenixBOT
muted wing
#

This would be a good use case for a comprehension.

#

The for loop is essentially creating multiple assignments, but it's not reassigning to within the list, itself.

#

It's just pointing item at the lowercased versions of each string.

#

!e py items = ['ApPle', 'pEaR', 'oRaNgE'] result = [item.lower() for item in items] print(items) print(result)

cloud phoenixBOT
muted wing
#

Here is an example of something called a list comprehension.

#

It's an in-line for loop syntax designed to specify a process for creating the elements the list should have.

#

Thinkpy items = [...] result = [] for item in items: result.append(item.lower())

#

This creates a new list. You could even overwrite the items variable, here.

#

But only with the comprehension, not the indented for.

#

If I wanted to edit the list "in place" I would use a generator expression and assignment to slice.

#

!e py items = ['ApPle', 'pEaR', 'oRaNgE'] items[:] = (item.lower() for item in items) print(items)

cloud phoenixBOT
muted wing
#

This has the advantage of keeping the list object, itself.

muted wing
#

!e py items = ['ApPle', 'pEaR', 'oRaNgE'] items = [item.lower() for item in items] print(items)This would be fine, too, but it is a different list.

cloud phoenixBOT
tough yew
wheat wasp
#

!e Just one more example to keep the list object

names = ["SAM", "TOM", "CAM", "KING", "ANDY"]

for i, item in enumerate(names):
    names[i] = item.lower()

print(names)
cloud phoenixBOT
cloud phoenixBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.