Learn
Lists and Functions
Modifying an element of a list in a function

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.
Folder Icon

Take this course for free

Already have an account?