Kidus Asmare Ayele
Kidus Asmare Ayele

Reputation: 93

Is there a way to automatically split a phrase into a list by its spaces?

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:

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

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

Upvotes: 4

Views: 79

Answers (4)

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

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']

Upvotes: 2

Chris Holland
Chris Holland

Reputation: 579

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

Upvotes: 5

targhs
targhs

Reputation: 1797

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

Upvotes: 0

Cargo23
Cargo23

Reputation: 3189

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.

Upvotes: -1

Related Questions