#๐Ÿ”’ need help with a task (very new to coding)

65 messages ยท Page 1 of 1 (latest)

drowsy mauve
#

how do I add a string into a list?

dense scrollBOT
#

@drowsy mauve

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.

drowsy mauve
#

I tried appending a string into my list, but it doesn't work

slow warren
drowsy mauve
#

aight just a sec

slow warren
drowsy mauve
#

yes

#

let me just check something

slow warren
#

sure

drowsy mauve
#
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

slow warren
#

hmm

#

can you try

longest = [listen[0]]
drowsy mauve
#

ok

#

I now have problems with the remove

slow warren
#

You might also want to double check the longest.remove(listen[0]) line

#

What is it that you actually want to remove?

drowsy mauve
#

everything inside longest

slow warren
#

You could just do longest.pop() because it looks like there's only going to be at most 1 item in the list

drowsy mauve
#

ok

last hound
#

Cant you do longest = item instead of longest.remove(listen[0]) && longest.append(item)?

drowsy mauve
#

no

#

because if there are 2 words that are the same length, I have to add both

slow warren
#

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```
drowsy mauve
#

ok

#

but why not the or?

last hound
#

Because you have to have different handling when its larger and when its the same len

slow warren
#

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

drowsy mauve
#

ok

#

how does pop work?

slow warren
# drowsy mauve how does pop work?

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

slow warren
drowsy mauve
#
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

slow warren
#

Yeah, you're doing the if statements wrong

fathom girder
#

pop doesnt know which item to remove.. you must specify.. if always the last or first.. maybe deque would be useful?

slow warren
#

it should be len(longest[0])

slow warren
fathom girder
#

i did not know that, thx

slow warren
#

You want to compare the item with any word in the longest list. Not with the list itself.

drowsy mauve
#

yeah I fixed it

slow warren
#

Does it work as expected now?

#

what's your new code?

drowsy mauve
#
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

slow warren
#

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)

drowsy mauve
slow warren
#

You can check if it isn't empty before doing something

#

If it's empty, just append the item.

drowsy mauve
#

how do I check if the list is empty?

slow warren
drowsy mauve
#

ok thanks

#

it works now

low scroll
#

or simply if not your_list: ...

dense scrollBOT
#
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.