Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
List basics
Lists with if statements and loops
Lists in functions
18. Modifying lists in functions
Summary

Instruction

Good! Now, we need to introduce an important concept related to passing lists as arguments to functions: any changes to the list made inside functions will affect your original list outside the function.

def delete_first_element(input_list):
  del input_list[0]
sample_list = [1, 2, 3, 4, 5]
delete_first_element(sample_list)

The delete_first_element() function above will modify your sample_list by deleting its first element.

Technically, we can say that Python passes lists by reference. There are also other languages that pass copies of lists so that the original lists are never modified (this, in turn, is called passing by value).

Exercise

Run the template code. print(sample_list) will be executed twice: before and after the delete_first_element() invocation.

As you can see, the function deleted the first element from our original list.