#๐ why .lower() no work?
40 messages ยท Page 1 of 1 (latest)
@buoyant mesa
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.
Your not actually setting the Items in the list, I'm pretty sure
You want to make another list that has the lower case items, and then append the items from the upper case list
this happens because names[0], names[1] ....
are loaded into item and only item is converted to uppercase.
without any change to names[0] names[1] etc..
What you can do is
for i in range(len(names)):
names[i]=names[i].lower()
okay this worked thank you very much
why it dont change names[0] tho and only item>
?
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
because strings are not mutable datatype
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
a little better explanation....
u have for item in names loop, which essentially does item = names[0], item = names[1], etc.
so, lower down u do item = item.lower(), but it DOES NOT affect the original value in names. We can even check it out using just normal variables
!e
original_name = 'ALEX'
name = original_name
name = name.lower()
print(f'{original_name=}, {name=}')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
original_name='ALEX', name='alex'
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | ['ApPle', 'pEaR', 'oRaNgE']
002 | ['apple', 'pear', 'orange']
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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['apple', 'pear', 'orange']
This has the advantage of keeping the list object, itself.
!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.
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['apple', 'pear', 'orange']
names = list(map(str.lower, names))
!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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['sam', 'tom', 'cam', 'king', 'andy']
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.