Modifying an element of a list in a function


#1



Change list_function so that:

Add 3 to the item at index one of the list.
Store the result back into index one.
Return the list.


def list_function(x):
    return x[1]

n = [3, 5, 7]
print list_function(n)


#2

Hi @juicypine,

To add 3 to a variable named a you could do either this ...

a += 3

... or this ...

a = a + 3

For this exercise, do either one of the above for x[1].

Thereafter, you need to return the entire list, x, rather than only x[1].


#3

hi @appylpye !
Just wondering what is the difference between returning the list x after adding the 3 in a separate line versus returning the line x[1] += 3 ?


#4

The line ...

x[1] += 3

... is an assignment statement. That statement prescribes an action to be taken, but in its entirety, the statement is not an expression that can be evaluated. Therefore, it cannot be used to specify a return value.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.