#๐ need help with a task (very new to coding)
65 messages ยท Page 1 of 1 (latest)
@drowsy mauve
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.
I tried appending a string into my list, but it doesn't work
Can you paste your code?
aight just a sec
Still here?
sure
def length_of_longest (listen):
longest = listen[0]
for item in listen:
if len(item) > len(longest) or len(item) == longest:
longest.remove(listen[0])
longest.append(item)
return (longest)
print (length_of_longest(["forste", "andfre", "re"]))
its supposed to return the longest strings
You might also want to double check the longest.remove(listen[0]) line
What is it that you actually want to remove?
everything inside longest
You could just do longest.pop() because it looks like there's only going to be at most 1 item in the list
ok
Cant you do longest = item instead of longest.remove(listen[0]) && longest.append(item)?
Well then you should probably change your code.
Right now you're removing the word if it's the same length as well, which is why they suggested that.
You could change the function to have an if-elif
def find_the_longest(listen):
longest = []
for item in listen:
if len(item) > len(longest[0]):
do something
elif len(item) == len(longest[0]):
do something else```
Because you have to have different handling when its larger and when its the same len
You want to keep the list if it's the same length, but you want to empty the list if it's not.
Therefore you should be using an if-elif
it removes the last item in the list, but since you want to clear the entire list if it's shorter, you can just do longest = [] to make it empty
ok
Have you got what you were trying to do?
def length_of_longest (listen):
longest = [listen[0]]
for item in listen:
if len(item) > len(longest):
longest = []
longest.append(item)
elif len(item) == len(longest):
longest.append(item)
return (longest)
print (length_of_longest(["forste", "andfrfe", "re"]))
it seems to print the smallest
Yeah, you're doing the if statements wrong
pop doesnt know which item to remove.. you must specify.. if always the last or first.. maybe deque would be useful?
it should be len(longest[0])
pop always defaults to removing (and returning) the last item in the list
i did not know that, thx
Make sure you're doing len(longest[0]) when comparing
You want to compare the item with any word in the longest list. Not with the list itself.
yeah I fixed it
def length_of_longest (listen):
longest = [listen[0]]
for item in listen:
if len(item) > len(longest[0]):
longest = []
longest.append(item)
elif len(item) == len(longest[0]):
longest.append(item)
return (longest)
print (length_of_longest(["tfdr", "fjde", "re"]))
it seems to print the first string twice
Yeah, don't do the longest = [listen[0]] line
just do longest = []
You could also just make it a set (if you don't want duplicates)
then the if statement doesn't work, cos you have nothing to compare item to
Then change the if statements
You can check if it isn't empty before doing something
If it's empty, just append the item.
how do I check if the list is empty?
You could check if the length of the list is 0
or simply if not your_list: ...
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.