4

I am trying to construct a method that takes in a phrase and splits it according to its spaces and then returns a list of words.

Example:

  • Input: 'Hello there how are you'
  • Output: ['Hello', 'there', 'how', 'are', 'you']

Currently, I am getting this (the last word is missing), because it will not append the last word as long as it can't find a space after it

  • Output: ['Hello', 'there', 'how', 'are']

I know that I can do this through the split method but I just wanted to know if I could do the same thing using my code.

def words(phrase):
    my_list = []
    final_word = ''
    for word in phrase:
        if word == ' ':
            my_list.append(final_word)
            final_word = ''
        else:
            final_word = final_word + word
    return my_list

4 Answers4

5

I know that I can do this through the split method but I just wanted to know if I could do the same thing using my code.

In your code you are missing the last part because it is not followed by a space. If you add an extra element if anything comes after the last space you should be fine. Like this:

def words(phrase):
    my_list = []
    final_word = ''
    for word in phrase:
        if word == ' ':
            my_list.append(final_word)
            final_word = ''
        else:
            final_word = final_word + word
    if final_word:
        my_list.append(final_word)
    return mylist
Chris Holland
  • 356
  • 2
  • 10
2

You can change your code to this:-

def words(phrase):
    my_list = []
    final_word = ''
    for word in phrase:
        if word == ' ':
            print(my_list)
            my_list.append(final_word)
            final_word = ''
        else:
            final_word = final_word + word
    if final_word != '':
        my_list.append(final_word)
    return my_list

Output:-

['Hello', 'there', 'how', 'are', 'you']
Dhaval Taunk
  • 1,668
  • 1
  • 7
  • 17
  • 1
    The OP explicitly states "I know that I can do this through the split method but I just wanted to know if I could do the same thing using my code." – MisterMiyagi Jun 10 '20 at 17:40
0

I guess nobody read your question fully. So basically you want to write the split method by yourself.

Your implementation is correct but you miss one single line. Here's the complete code

def words(phrase):
    my_list = []
    final_word = ''
    for word in phrase:
        if word == ' ':
            my_list.append(final_word)
            final_word = ''
        else:
            final_word = final_word + word
    my_list.append(final_word) # This is what you are missing
    return my_list

One thing to make note here is the scope of variables declared inside the for loop persist even outside the for loop. You can even use word variable outside the for loop if needed.

You may find this helpful: Scoping in Python 'for' loops

targhs
  • 448
  • 1
  • 6
  • 14
-1

Here is the answer:

def words(phrase):
   return phrase.split(" ")

And here is what was wrong with your existing code:

Your variable word is actually a character. Whenever you see a space in the phrase, you are adding the current value of final_word to my_list. However, on your last word, the string ends, and there is no ' ' after it, so you never add that word to the list.

Cargo23
  • 2,267
  • 13
  • 22