# 5.7 Passing Lists to Functions¶

### Passing an Entire List to a Function¶

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]

### Passing a Tuple to a Function¶

- When 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)
    <ipython-input-7-9339741cd595> in <module>
    ----> 1 modify_elements(numbers_tuple)

    <ipython-input-1-aa30cff7ee99> in modify_elements(items)
          2     """"Multiplies all element values in items by 2."""
          3     for i in range(len(items)):
    ----> 4         items[i] *= 2

    TypeError: 'tuple' object does not support item assignment

### A Note Regarding Tracebacks¶

- A traceback shows code that led to an exception.
- When an exception occurs in a single-line snippet, it’s always preceded by `----> 1`, indicating that line 1 (the snippet’s only line) caused the exception.
- Multiline snippets like a function definition show consecutive line numbers starting at 1.
- The last line of code shown with `---->` caused the exception.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.