Appearance
Unpacking Function Arguments in Python
Unpack the list
We can use *
to unpack the list.
python
# A function takes 4 arguments
def fun(a, b, c, d):
print(a, b, c, d)
# A list with four elements
my_list = [1, 2, 3, 4]
# Unpacking list into four arguments
fun(*my_list)
Unpack the dictionary
We can use **
to unpack the dictionary.
python
# A function takes 3 arguments
def fun(a, b, c):
print(a, b, c)
# A dictionary with the keys equal with the parameter names.
d = {'a':2, 'b':4, 'c':10}
# Unpacking dictionary into three arguments
fun(**d)