Skip to content

Python splat operator

  • by

The “splat” operator in Python is a term often used to refer to the asterisk (*) symbol when used in function calls, unpacking iterables, and defining function parameters. It’s officially known as the “unpacking operator” or “extended unpacking” operator. The splat operator has two main use cases:

Unpacking Iterables: You can use the splat operator to unpack elements from an iterable (like a list, tuple, or string) and pass them as separate arguments to a function or use them in assignments.

numbers = [1, 2, 3, 4, 5]
print(*numbers)  # Unpacks and prints each element separately

# Unpack elements and pass as arguments to a function
def add(a, b, c):
    return a + b + c

result = add(*numbers[:3])  # Unpacks first three elements from 'numbers'
print(result)  # Output: 6

Collecting Arguments in Functions: In function definitions, the splat operator can be used to collect a variable number of arguments into a tuple (or list) parameter.

def print_args(*args):
    for arg in args:
        print(arg)

print_args(1, 2, 3, 4)  # Prints each argument on a new line

def calculate_sum(*numbers):
    return sum(numbers)

total = calculate_sum(1, 2, 3, 4, 5)  # Calculates sum of all arguments
print(total)  # Output: 15

There’s also a related operator, the double asterisk (**), which is used to unpack dictionary key-value pairs into keyword arguments in function calls and to create dictionaries from key-value pairs.

def example_function(**kwargs):
    for key, value in kwargs.items():
        print(key, value)

example_function(name="Alice", age=30, city="Wonderland")

# Creating a dictionary using double asterisk
info = {'name': 'Bob', 'age': 25}
user_info = {'username': 'bob123', **info}
print(user_info)

In summary, the splat operator (*) and double splat operator (**) in Python have various uses, such as unpacking iterables, collecting function arguments, and creating dictionaries.

Python splat operator example

Here are some examples of how the splat operator (*) can be used in Python:

1. Unpacking Iterables:

# Unpacking a list into individual elements
numbers = [1, 2, 3, 4, 5]
print(*numbers)  # Output: 1 2 3 4 5

# Unpacking a tuple into separate function arguments
def add(a, b, c):
    return a + b + c

result = add(*numbers[:3])  # Unpacks first three elements from 'numbers'
print(result)  # Output: 6

2. Collecting Function Arguments:

# Collecting multiple arguments using the splat operator
def print_args(*args):
    for arg in args:
        print(arg)

print_args(1, 2, 3, 4)

3. Unpacking Dictionary Key-Value Pairs:

# Unpacking dictionary key-value pairs as function keyword arguments
def example_function(**kwargs):
    for key, value in kwargs.items():
        print(key, value)

example_function(name="Alice", age=30, city="Wonderland")

4. Creating Dictionaries:

# Creating a new dictionary by combining two dictionaries using the double splat operator
info = {'name': 'Bob', 'age': 25}
user_info = {'username': 'bob123', **info}
print(user_info)

Output:

Python splat operator

These examples demonstrate different ways in which the splat operator can be used to unpack elements from iterables, collect function arguments, unpack dictionary key-value pairs, and create dictionaries.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *