revisionweb
revisionweb

Reputation: 11

Remove first word from the sentence and return remaining string

Write a function that is given a phrase and returns the phrase we get if we take
out the first word from the input phrase. For example, given ‘the quick brown fox’, your function should return ‘quick brown fox’ This is my code:

def whatistheremainder(v):
    remainderforone = v.split(' ', 1)
    outcome = remainderforone[1:]
    return outcome

Instead of getting a sensible output like: 'quick brown fox' I am getting something like this:

['quick brown fox']

Please help

Upvotes: 1

Views: 1701

Answers (5)

mohsine maiet
mohsine maiet

Reputation: 24

 def whatistheremainder(v):
    remainderforone = v.split(' ', 1)
    outcome=v if len(remainderforone)== 1 else ''.join(remainderforone[1:])
    return outcome

this line 'outcome=v if len(remainderforone)== 1 else ''.join(remainderforone[1:])' check if the length of the list contains all the words if the length is equal to 1 its mean there is only one word so the outcome will equal to v (the word entered) else mean there is more then one word the outcome will equal to the entered string without the first word

Upvotes: 0

mhawke
mhawke

Reputation: 87124

[1:] takes a slice from the list, which is itself a list:

>>> remainderforone
['the', 'quick brown fox']
>>> remainderforone[1:]
['quick brown fox']

Here the slice notation [1:] says to slice everything from index 1 (the second item) to the end of the list. There are only two items in the list, so you get a list of size one because the first item is skipped over.

To fix just extract a single element of the list. We know that the list should contain 2 elements, so you want the second item so just use index 1:

>>> remainderforone[1]
'quick brown fox'

As a more general solution you might want to consider using str.partition():

for s in ['the quick brown fox', 'hi there', 'single', '', 'abc\tefg']:
    first, sep, rest = s.partition(' ')
    first, sep, rest

('the', ' ', 'quick brown fox')
('hi', ' ', 'there')
('single', '', '')
('', '', '')
('abc\tefg', '', '')

Depending on how you want to handle those cases where no partitioning occurred you could just return rest, or possibly first:

def whatistheremainder(v):
    first, sep, rest = v.partition(' ')
    return rest

for s in ['the quick brown fox', 'hi there', 'single', '', 'abc\tefg']:
    whatistheremainder(s)

'quick brown fox'
'there'
''
''
''

Or you could argue that the original string should be returned if no partitioning occurred because there was no first word to remove. You can use the fact that sep will be an empty string if no partitioning occurred:

def whatistheremainder(v):
    first, sep, rest = v.partition(' ')
    return rest if sep else first

for s in ['the quick brown fox', 'hi there', 'single', '', 'abc\tefg']:
    whatistheremainder(s)

'quick brown fox'
'there'
'single'
''
'abc\tefg'

Upvotes: 1

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

Your logic can be further simplified in one line by setting maxsplit param as 1 for str.split() function as:

>>> my_string = 'the quick brown fox'

>>> my_string.split(' ', 1)[1]
'quick brown fox'

This will raise IndexError if your string is with one or no word.

Another alternative using string slicing with list.index(...) as:

>>> my_string[my_string.index(' ')+1:]
'quick brown fox'

Similar to earlier solution, this one will also not work for one or no word string and will raise ValueError exception.

To handle strings with one or no word, you can utilise the first solution using maxsplit param, but access it as list using list slicing instead of index and join it back:

>>> ''.join(my_string.split(' ', 1)[1:])
'quick brown fox'

Issue with your code is that your need to join the list of strings you are sending back using ' '.join(outcome). Hence, your function will become:

def whatistheremainder(v):
    remainderforone = v.split(' ', 1)
    outcome = remainderforone[1:]
    return ' '.join(outcome)

Sample run:

>>> whatistheremainder('the quick brown fox')
'quick brown fox'

You above logic to split the string into words and joining it back skipping first word can also be converted into one line as:

>>> ' '.join(my_string.split()[1:])
'quick brown fox'

Upvotes: 1

mohsine maiet
mohsine maiet

Reputation: 24

 def whatistheremainder(v):
    remainderforone = v.split(' ', 1)
    outcome = ''.join(remainderforone[1:])
    return outcome

Upvotes: -1

Is this what you wanted to get

def whatistheremainder(v):
    remainderforone = v.split(' ', 1)
    outcome = remainderforone[1:][0]
    return outcome
print(whatistheremainder('the quick brown fox'))

Output

quick brown fox

Upvotes: 1

Related Questions