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
  1. We create a list called numbers.
  2. We use the double_first function to modify that list.
  3. 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

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?