Learn
Modifying an element in a list in a function is the same as if you were just modifying an element of a list outside a function.
def double_first(n): n[0] = n[0] * 2 numbers = [1, 2, 3, 4] double_first(numbers) print numbers
- We create a list called
numbers
. - We use the
double_first
function to modify that list. - Finally, we print out
[2, 2, 3, 4]
When we pass a list to a function and modify that list, like in the double_first
function above, we end up modifying the original list.
Instructions
1.
Change list_function
to:
- Add 3 to the item at index
1
of the list. - Store the result back into index
1
. - Return the list.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.