DEV Community

Discussion on: Python: What are *args and **kwargs?

Collapse
 
serg_syd profile image
Sergio • Edited

What is the difference if I just used list or dictionary as arguments?

def my_hobbies(hobbies_list):
print("My hobbies: " + ", ".join(hobbies_list))

Collapse
 
adamlombard profile image
Adam Lombard • Edited

In our simple example, there isn't much of a difference.

In a real-world example, the *args and *kwargs syntax give us more options in how we accept data into our function.

The decision is largely one between:
A) forcing the use of structured data, and...
B) not needing to know the structure in advance.

In your example, we must always pass in a list or dictionary, even if we are only passing in a single hobby. (This approach is not necessarily bad -- it may, in fact, be the right solution.) If, instead, we allow variable-length arguments -- by using the *args or *kwargs syntax -- then we do not need to structure the data in advance.

A more in-depth discussion related to your question can be found here.