Search code examples
pythonlistloopsfor-loopplural

Printing plurals from a list


Pretty noobie, but I'm trying to write a function that prints all the plural words in a list of words

So output would be:

 >>> printPlurals(['computer', 'computers', 'science,', 'sciences'])
 computers
 sciences

and this is what I have so far but I'm not getting any output. Any help would be great. ty.

def printPlurals(list1):
    plural = 's'

    for letter in list1:
        if letter[:-1] == 's':
            return list1

Solution

  • You're really close, but you're mixing a few things up. For starters, you don't need to have the plural variable. You're not using it anyway. Secondly, from a naming standpoint, it doesn't matter that you've named the variable letter as you have, but it implies that maybe you think you're looping through letters. Since you're actually looping through the members of the list list1, you're considering a word at each iteration. Finally, you don't want to return the list. Instead, I think you want to print the word that has been confirmed to end in s. Try the following. Good luck!

    def print_plurals(word_list):
        for word in word_list:
            if word[-1] == 's':
                print word
    

    In case you're interested in doing something a little more interesting (or "Pythonic", arguably), you could form the list of plurals through a list comprehension as follows:

    my_list = ['computer', 'computers', 'science', 'sciences']
    plural_list = [word for word in my_list if word[-1]=='s']