What is the difference between *args and **kwargs

Can you please explain what is the difference between *args and **kwargs in Python?

*args is used to pass the variable number of arguments and iterate over the same. **kwargs is used to pass a key-value pairs, variable-length arguments.

Below are the examples of the same

def iterate_over_list(*args):
    for arg in args:
        print(arg)
        
iterate_over_list(1, 2, 3)

Output -

1
2
3

def iterate_over_key_value_pairs(**kwargs):
    for key, value in kwargs.items():
        print(key, value)
        
iterate_over_key_value_pairs(a=2, b=2)

Output -

a 2
b 2

1 Like