Beyond Python Logo

Default parameters using Lists in Python

Written by Matthew Yeager
2-minute read (300 words)
Published: Mon Sep 30 2019
Default Parameters in Python

Python allows you to provide default values for arguments. You can define a function and provide a value for when users do not pass one in. This is most useful when there is a value which handles the most common use cases.

def divided_by_three(numbers, evens_only = True):
    # Divid 'numbers' by 3, providing the result.
    # 'evens_only' determines which values are returned.
    for n in numbers:
        third_of_value = round(n / 3, 2)
        if not evens_only or (int(third_of_value) % 2 == 0 and int(third_of_value * 100) % 2 == 0):
            yield third_of_value
list(divided_by_three([12, 3, 14, 88, 66]))

> [4.0, 22.0]
list(divided_by_three([12, 3, 14, 88, 66], evens_only=True))

> [4.0, 22.0]
list(divided_by_three([12, 3, 14, 88, 66], False))

> [4.0, 1.0, 4.67, 29.33, 22.0]
list(divided_by_three([12, 3, 14, 88, 66], evens_only=False))

> [4.0, 1.0, 4.67, 29.33, 22.0]
Default lists, objects, and functions

We could decide to provide a default list for users when a function is called.

Warning: this won't work as expected!

def double_list_items(items = ['x', 'y']):
    # Duplicate items in the list
    items *= 2
    return items
double_list_items()

> ['x', 'y', 'x', 'y']

We were able to update the list to have the items appear twice.

double_list_items()

> ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'y']
double_list_items(['a', 'p', 'p', 'l', 'e'])

> ['a', 'p', 'p', 'l', 'e', 'a', 'p', 'p', 'l', 'e']
double_list_items()

> ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'y', 'x', 'y', 'x', 'y', 'x', 'y', 'x', 'y']

What happened is our default list is evaluated once and stays in memory, reused each time we call the function. When we change the value of items the new value persists for each new call.

Default list arguments in functions

When we want to provide default values for lists, we need to first check if they were not passed in, and only then set them. This way the default value is evaluated and set each time the function is called, instead of once and then shared.

def duplicate_list_items(items = None):
    # Duplicate items with a default
    items = items or ['z', 'k']
    items *= 2
    return items
duplicate_list_items()

> ['z', 'k', 'z', 'k']
duplicate_list_items()

> ['z', 'k', 'z', 'k']
duplicate_list_items(['c', 'a', 't'])

> ['c', 'a', 't', 'c', 'a', 't']



Questions, Comments, Concerns?

Thanks for reading! If you've made it this far then you are probably interested in the material that we will be producing. We have an idea of what we believe will be most valuable to our readers, but hearing from you directly would be even better.

Send us an email at questions@beyondpython.com or reach out to us on twitter @BeyondPython

If you have a topic that you are struggling with, a file that you can't seem to work with, or even a dataset that just seems impossible to wrangle, then please let us know. We want to provide you with useful and practical information so you can start using Python today.

Beyond Python Visual Newsletter

Enjoying the content? We send step-by-step visual Python tutorials to your inbox! Be notified when new content is available by the Beyond Python team.



Disclosures & Privacy
All Rights Reserved
© 2019 Beyond Python