[Tutor] Tutor Digest, Vol 100, Issue 58

Emile van Sebille emile at fenx.com
Tue Jun 26 01:26:55 CEST 2012


On 6/25/2012 3:12 PM Mike Nickey said...
>>> The problem with this code is that it only gets the first word beginning
>>> with x and the others remaisn on the original list, sorted at the end. I've
>>> tested on terminal many parts of the code and it whas fine, but when I run
>>> it complete, it does not work.
>>>
>>> Following is the solution of the problem. I can understand it, I just
>>> can't understand why my code does not work.
>>>
>>> #############################
>>> def front_x(words):
>>>    x_list = []
>>>    other_list = []
>>>    for w in words:
>>>      if w.startswith('x'):
>>>        x_list.append(w)
>>>      else:
>>>        other_list.append(w)
>>>    return sorted(x_list) + sorted(other_list)
>>> ##############################
>
> I did the same exercise so maybe I can assist here. From what I see, the line...
> if w.startswith('x'): seems to be ineffective.

Why do you think so?

> Try using the operator that checks for equality such as
> if w[0] == 'x':

This does the same thing and works even when w is an empty string.

Emile



More information about the Tutor mailing list