1

I want to use the following function in order mutate the elements of two lists: If a list element starts with a "U", the element should be mutated to "1", otherwise the list element should be mutated to "0". Here is the code:

def measure(p):
    for x in range(len(p)):
        if p[x][0] == 'U':
            p[x] = 1
        else:
            p[x] = 0
        return p

print measure(['Dave','Sebastian','Katy'])
print measure(['Umika','Umberto'])

The correct result should be

[0, 0, 0]
[1, 1]

But the current code produces:

[0, 'Sebastian', 'Katy']
[1, 'Umberto']

It seems that the iteration stopped after the first element. How can I solve this problem?

1
  • Your function terminates after the first loop, because of " return p" statement Commented Aug 9, 2018 at 14:06

5 Answers 5

4

Your function terminates after the first loop, because you have an unconditional return inside that loop. Move the return to the outside of the loop.

def measure(p):
    for x in range(len(p)):
        if p[x][0] == 'U':
            p[x] = 1
        else:
            p[x] = 0
    return p

print measure(['Dave','Sebastian','Katy'])
print measure(['Umika','Umberto'])

Result:

[0, 0, 0]
[1, 1]
Sign up to request clarification or add additional context in comments.

1 Comment

return [0 if x[0] != 'U' else 1 for x in p]
0

Know that return always terminates a function when executed. In the first loop of your for loop, your function executes the return statement and end the function measure. That's why only the first element of p is changed to 0 or 1.

Just put the return statement outside the for loop

def measure(p):
    for x in range(len(p)):
        if p[x][0] == 'U':
            p[x] = 1
        else:
            p[x] = 0
    return p

Comments

0

As a bonus, your function is quite simple, and can be made in a one-liner as follow:

def f(L):
    return [1 if elt[0].lower() == "u" else 0 for elt in L]

f(['Umika','Umberto'])
Out[27]: [1, 1]

f(['Dave','Sebastian','Katy'])
Out[28]: [0, 0, 0]

Comments

0

The identation of the return statement is inside the for loop. That means the code stops after modifying only the first element.

Put the return statement at the same identation level as the for statement and it should work.

Comments

0

Your return statement is indented too far - that's why it is stopping after the first element. It should be indented at the same level as the for x... line. Try:

>>> def measure(p):
...     for x in range(len(p)):
...         if p[x][0] == 'U':
...             p[x] = 1
...         else:
...             p[x] = 0
...     return p
...
>>> print measure(['Dave','Sebastian','Katy'])
[0, 0, 0]
>>> print measure(['Umika','Umberto'])
[1, 1]

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.