Exercise 10: returning list or directly returning action


in exercise 10 "Modifying an element of a list in a function," I was wondering why the two codes below result in different outcomes

def list_function(x):
    x[1] += 3
    return x

n = [3, 5, 7]

versus this one:

def list_function(x):
    return x[1] += 3

n = [3, 5, 7]

Thanks in advance! :slight_smile:


In first program. You're changing the value of 2nd element of list then returning the list.
In second program It will throw an error because return statement can't return a statement but expressions that reduce/evaluates down to some value.

x is a list here and x[index] is item of the list


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