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 = n * 2 numbers = [1, 2, 3, 4] double_first(numbers) print numbers
- We create a list called
- We use the
double_firstfunction 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.
- Add 3 to the item at index
1of the list.
- Store the result back into index
- Return the list.