Can you pass a list to a function in python?
This chapter is from the book Show
5.7 Passing Lists to FunctionsIn the last chapter, we mentioned that all objects are passed by reference and demonstrated passing an immutable object as a function argument. Here, we discuss references further by examining what happens when a program passes a mutable list object to a function. Passing an Entire List to a FunctionConsider the function modify_elements, which receives a reference to a list and multiplies each of the list’s element values by 2: In [1]: def modify_elements(items): ...: """"Multiplies all element values in items by 2.""" ...: for i in range(len(items)): ...: items[i] *= 2 ...: In [2]: numbers = [10, 3, 7, 1, 9] In [3]: modify_elements(numbers) In [4]: numbers Out[4]: [20, 6, 14, 2, 18] Function modify_elements’ items parameter receives a reference to the original list, so the statement in the loop’s suite modifies each element in the original list object. Passing a Tuple to a FunctionWhen you pass a tuple to a function, attempting to modify the tuple’s immutable elements results in a TypeError: In [5]: numbers_tuple = (10, 20, 30) In [6]: numbers_tuple Out[6]: (10, 20, 30) In [7]: modify_elements(numbers_tuple) ------------------------------------------------------------------------- TypeError Traceback (most recent call last) Recall that tuples may contain mutable objects, such as lists. Those objects still can be modified when a tuple is passed to a function. A Note Regarding TracebacksThe previous traceback shows the two snippets that led to the TypeError. The first is snippet [7]’s function call. The second is snippet [1]’s function definition. Line numbers precede each snippet’s code. We’ve demonstrated mostly single-line snippets. When an exception occurs in such a snippet, it’s always preceded by ----> 1, indicating that line 1 (the snippet’s only line) caused the exception. Multiline snippets like the definition of modify_elements show consecutive line numbers starting at 1. The notation ----> 4 above indicates that the exception occurred in line 4 of modify_elements. No matter how long the traceback is, the last line of code with ----> caused the exception. You can pass a list as an argument of Python function the same way as any other type and it will be the same type inside the function. Take a look at the following example:
There is a list of three numbers. You can pass this list as an argument and display all its elements one by one: 1 2 3 [1, 2, 3] If you print a list after the function execution, it will print the same numbers. Because lists are mutable, when you pass a list as an argument, you create a reference to this list and not a copy.
If you print the list it will display numbers: 1, 2, 3. After you modify this list, the modification will apply to the list outside the function because it was a reference and not a copy. [1, 2, 3] [8, 2, 3] [8, 2, 3] Creating a copy of a listIf you want to modify the list as a copy of the one inside parameter, you can use the copy function.
Now, if you run the code, the modification inside the function doesn’t apply to the my_list list, because the function operates on a copy and not on the original object. [1, 2, 3] [8, 2, 3] [1, 2, 3] In Python, you can unpack This article describes the following contents.
Note that if See the following articles for basic usage of functions in Python, default arguments, and variable-length arguments with
Unpack list and tuple with *When specifying a list or tuple with
Lists are used in the following sample code, but the same applies to tuples. If the number of elements does not match the number of arguments,
With default argumentsIf the function has default arguments, the default arguments are used if the number of elements is insufficient. If there are many elements,
With variable-length argumentsIf the function has a variable-length argument (
Unpack dict (dictionary) with **By adding
If there are not enough keys or keys that do not match the argument names,
With default argumentsIf the function has default arguments, only the value of the argument name matching the dictionary key is updated. If a key does not match the argument name,
With variable-length argumentsIf the function has a variable-length argument (
How do you add a list to a function in Python?How to add Elements to a List in Python. append() : append the element to the end of the list.. insert() : inserts the element before the given index.. extend() : extends the list by appending elements from the iterable.. List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.. Can you pass a list as a parameter?You should always avoid using List
How do you pass a number and a list as an argument in Python?Use the * Operator in Python
The * operator can unpack the given list into separate elements that can later be tackled as individual variables and passed as an argument to the function.
|