Iterating at Once with the Asterisk (*) – Unpacking in Data Science 2026
The asterisk (* and **) is a powerful Python feature for unpacking iterables and dictionaries. In data science, mastering unpacking helps you write cleaner, more concise, and more Pythonic code when working with lists, tuples, function arguments, and data structures.
TL;DR — Asterisk Usage
*– Unpacks iterables (lists, tuples, etc.)**– Unpacks dictionaries (keyword arguments)- Very useful in function calls, list creation, and data manipulation
1. Basic Unpacking with *
numbers = [10, 20, 30, 40]
# Unpacking into variables
a, b, *rest = numbers
print(a, b, rest) # 10 20 [30, 40]
# Using * in function calls
def calculate_total(*values):
return sum(values)
total = calculate_total(*numbers)
print(total)
2. Common Data Science Use Cases
import pandas as pd
df = pd.read_csv("sales_data.csv")
# 1. Unpacking column names
cols = ["order_date", "customer_id", "amount", "region"]
date_col, id_col, *metric_cols = cols
# 2. Passing multiple arguments to a function
def train_model(*feature_columns):
print(f"Training with features: {feature_columns}")
train_model(*metric_cols)
# 3. Creating new lists with unpacking
new_features = ["profit", "margin", *metric_cols]
print(new_features)
3. Advanced Unpacking with ** (Dictionary Unpacking)
model_params = {
"n_estimators": 200,
"max_depth": 10,
"random_state": 42
}
# Unpacking dictionary as keyword arguments
def train_random_forest(**kwargs):
print("Training with parameters:")
for key, value in kwargs.items():
print(f" {key}: {value}")
train_random_forest(**model_params)
4. Best Practices in 2026
- Use
*to unpack iterables when calling functions or creating new lists - Use
**to unpack dictionaries as keyword arguments - Combine with
enumerate()andzip()for powerful one-liners - Use
*restto capture remaining elements when unpacking - Keep unpacking readable — avoid excessive complexity in one line
Conclusion
The asterisk operators (* and **) are powerful tools for unpacking data in Python. In data science, they help you write cleaner code when passing column lists to functions, creating new feature sets, and handling configuration parameters. When used appropriately, unpacking makes your code more concise and Pythonic while maintaining readability.
Next steps:
- Review your current code and look for opportunities to use
*and**unpacking to simplify function calls and list/dict operations