2

I have a list of words like the following:

old_list = ['houses','babies','cars','apples']

The output I need is:

new_list = ['house','baby','car','apple']

In order to do this, I came up with a loop:

new_list1 = []
new_list2 = []
for word in old_list:
    if word.endswith("ies"):
        new_list1[:0] = [word.replace("ies","y")] 
    elif word.endswith("s"):
        new_list2[:0] = [word.replace(' ','')[:-1]]

new_list = new_list1 + new_list2 # Order doesn't matter, but len(new_list) == len(old_list)

It's simply not working. I'm getting something like:

new_list = ['baby','house','babie','car','apple']

I'm sure I'm just doing one simple thing wrong but I can't see it. And I would use list.append() if there's an easy way to implement it.

Thanks!

5
  • 2
    I'm getting ['baby', 'apple', 'car', 'house'] with your code. Commented Oct 8, 2013 at 10:36
  • even i am getting same answer as @Bogdan ...and remove that space between new_list and 2!! Commented Oct 8, 2013 at 10:38
  • I'm checking my code again, I didn't realize that the mistake may be coming from something else. Sorry. Commented Oct 8, 2013 at 10:45
  • @Bogdan: I have a question though, how would you remove any words containing anything else than letters and dashes? Commented Oct 8, 2013 at 10:49
  • @marc, remove from where? I guess something along the lines of [s for s in l if re.match(r'[\-\w]*$', s)] (or with an analogous filter). Commented Oct 8, 2013 at 11:01

6 Answers 6

6

Firstly, word.replace("ies","y") is not a very good idea; sometimes you can find it in the middle of the word e.g. diesel

new_list = []
for word in old_list:
    if word.endswith("s"):
        if word.endswith("ies"):
            new_list.append(word[:-3] + "y")
        else:
            new_list.append(word[:-1])
Sign up to request clarification or add additional context in comments.

3 Comments

You hit the nail on the head. The issue is that I needed to check for s first.
How would you then replace all words containing "degree" (ie. "50-degrees" with the word "degree"? I tried new_list.append("degree"), but it still returns the whole word.
Sorry I am not sure I understand. If the old_list contains 50-degrees, the new one will contain 50-degree. If you need to remove 50-, then you need to formalize the rule, e.g. remove evertyhing before -.
5

This doesn't directly address your question (why your attempted code doesn't work as expected) but I suggest to not use a loop in the first place - instead, have a dedicated function for giving the singular form of a given english word, like:

def singular(word):
  # ...
  return singularForm

And then use a list comprehension

new_list = [singular(word) for word in old_list]

It's shorter and IMHO nicely communicates what you do: getting the singular form of every word in old_list instead of talking about how it's done (by looping over the list).

Comments

4

And I would use list.append() if there's an easy way to implement it.

I think you're misunderstanding how list.append() works. Remember that lists are mutable, so you don't need to do the new_list1[:0] = blah. You can just do:

new_list = []
for word in old_list:
    if word.endswith("ies"):
        new_list.append(word.replace("ies","y"))
    elif word.endswith("s"):
        new_list.append(word.replace(' ',''))

Also I'm not seeing what your second replace function should be. If you want to get rid of the 's' (for words such as 'houses'), you can use slicing:

>>> print 'houses'[:-1]
house

7 Comments

slicing won't turn babies into baby.
@FrerichRaabe I showed the slicing for the second replace. It's not intended for the -ies words
Ah sorry, I thought "second replace" would be the one you're not addressing with your slicing remark.
@FrerichRaabe I clarified it a bit :)
I believe he wanted to remove trailing spaces at the end of word and then to get rid of the s. Something like print 'houses '[:-1] -> house. If I'm right, it may me better written using strip: word.strip()[:-1]
|
0

You can do this easily in one line.

new_list = [word[:-1].replace('ie','y') for word in old_list]

Just be careful of words like 'pies' because the code will turn it into 'py'. Maybe that is Python trying to send us secret messages. ;)

Comments

0
#Use list comprehension to less code#
old_list = ['houses','babies','cars','apples']
new_list = ['house','baby','car','apple']
new_list+=[word.replace('ies','y') for word in old_list if word.endswith('ies')]

1 Comment

-1 Just dumping a snippet of code without any further explanation is not very educational.
0
old_list = ['cars', 'babies', 'computers', 'words']

def remove_s_ies(string):
    if string.endswith('ies'):
        return string[:-3]+'y'
    elif string.endswith('s'):
        return string[:-1]


new_words = [ remove_s_ies(word) for word in old_list ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.