Aimee Prater
Aimee Prater

Reputation: 23

How to add the word 'and' to the return of string list

I am getting the right out put:

Need my out put to look like this:

The 7 sight words for this week are new, barn, shark, hold, art, only, and eyes. The 2 sight words for this week are subtract, and add. The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again, and above. The only sight word for this week is question. There are no new sight words for this week!

but I'm wondering about the 'and' before the last word that needs to be added in the return. Is this appropriate?

This is the code:

wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes'],
            ['subtract', 'add'],
            ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above'],
            ['question'],
            []]

def createSentence(wordlist):
    if len(wordlist) > 1:
        wordlist[-1] = "and " + wordlist[-1]
        return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist)}.'
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

for lst in wordlist:
    print(createSentence(lst))

Upvotes: 2

Views: 240

Answers (3)

sahinakkaya
sahinakkaya

Reputation: 6056

You can join first n-1 elements with a , and add and nth element at the end:

if len(wordlist) > 1:
    return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist[:-1])} and {wordlist[-1]}.'

So that you don't need to manually add and before last word in your input lists.

Upvotes: 1

PossiblyAShrub
PossiblyAShrub

Reputation: 587

It sounds like you want to add "and " to the beginning of your last element in you array/list, but only if you have more than 1 word.

Python has a super-easy way to get the last element of an array/list.

wordlist[-1]

so to add "and" in front of that:

wordlist[-1] = "and " + wordlist[-1]

Your final code should end up looking like this

def createSentence(wordlist):
    if len(wordlist) > 1:
        wordlist[-1] = "and " + wordlist[-1]
        return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist)}.'
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51643

The case handling more then 1 list element simply has to join all but the least element with ',' and then add the last element:

wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only', 'and eyes'],
            ['subtract', 'and add'],
            ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 
             'right', 'again', 'and above'],
            ['question'],
            []]

def createSentence(wordlist):
    if len(wordlist) > 1:
        return (f'The {len(wordlist)} sight words for this week are'
                f' {", ".join(wordlist[:-1])} {wordlist[-1]}.')
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

for lst in wordlist:
    print(createSentence(lst))

Output:

The 7 sight words for this week are new, barn, shark, hold, art, only and eyes.
The 2 sight words for this week are subtract and add.
The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again and above.
The only sight word for this week is question.
There are no new sight words for this week!

If you do not want to add the 'and ' to your inputs, put it (including the comma) inside the format string:

wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only','eyes'],
            ['subtract', 'add'],
            ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again','above'],
            ['question'],
            []]

def createSentence(wordlist):
    if len(wordlist) > 1:
        return (f'The {len(wordlist)} sight words for this week are ' 
                f'{", ".join(wordlist[:-1])}, and {wordlist[-1]}.')
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

for lst in wordlist:
    print(createSentence(lst))

to get exactly the same output as above.


You use list slicing for that. More info: Understanding slice notation

Upvotes: 1

Related Questions