Unpacking Operator (* and **)
say it: “un-PAK-ing OP-er-ay-ter” /ʌnˈpækɪŋ ˈɑpəˌreɪtər/ or “STAR and DUB-ul STAR” /ˈstɑr ənd ˈdʌbəl ˈstɑr/
In a call, * spreads a list into arguments and ** spreads a dictionary.
What do * and ** do when calling a function?
In a call, * spreads a list out into separate arguments and ** spreads a dictionary out into keyword arguments.
def book_room(city, nights):
print(f"{nights} nights in {city}")
trip = ["Paris", 3]
book_room(*trip) # 3 nights in Paris
details = {"city": "Rome", "nights": 2}
book_room(**details) # 2 nights in Rome
Level: Freelancer · More Python terms at this level
Updated