What does asterisk mean in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The asterisk (*) in Python serves seven different purposes depending on context: multiplication, exponentiation (**), unpacking iterables in function calls, collecting arguments in function definitions (*args, **kwargs), unpacking in assignments, merging collections, and forcing keyword-only arguments. Knowing which meaning applies comes down to recognizing where the * appears: in an expression, a function signature, or a data structure literal.
1. Multiplication and Exponentiation
2. Unpacking Arguments in Function Calls
* unpacks an iterable into positional arguments. ** unpacks a dict into keyword arguments:
3. Collecting Arguments in Function Definitions
*args collects extra positional arguments into a tuple. **kwargs collects extra keyword arguments into a dict:
This is how functions like print() accept any number of arguments:
4. Keyword-Only Arguments
A bare * in a function signature forces all following parameters to be keyword-only:
Everything after * must be passed by name. This prevents ambiguous calls where argument order could be confused.
5. Unpacking in Assignments
The *variable collects all remaining elements into a list.
6. Merging Collections
7. In Type Hints
8. Positional-Only Parameters (Python 3.8+)
While / (not *) marks positional-only, they work together:
Summary Table
| Context | Syntax | Meaning |
| Expression | a * b | Multiplication |
| Expression | a ** b | Exponentiation |
| Function call | f(*list) | Unpack iterable into positional args |
| Function call | f(**dict) | Unpack dict into keyword args |
| Function def | def f(*args) | Collect extra positional args as tuple |
| Function def | def f(**kwargs) | Collect extra keyword args as dict |
| Function def | def f(*, key) | Force keyword-only arguments |
| Assignment | a, *b = [1,2,3] | Extended unpacking into list |
| Literal | [*a, *b] | Merge iterables |
| Literal | {**a, **b} | Merge dicts |
Common Pitfalls
- Mutable default with
*repeat:[[]] * 3creates[[], [], []]where all three lists are the SAME object.[[] for _ in range(3)]creates three independent lists. *argsis a tuple, not a list: You cannot useargs.append(). Convert withlist(args)if you need mutability.**kwargsloses order in Python < 3.7: In Python 3.7+, dicts preserve insertion order. In earlier versions,kwargsorder was undefined.- Unpacking in assignment requires iterable:
a, *b = 5raisesTypeErrorbecause5is not iterable. The right side must be a sequence. - Double unpacking in function call:
f(**d1, **d2)raisesTypeErrorifd1andd2share keys (in Python < 3.9). Usef(**{**d1, **d2})to merge first.
Summary
*has seven different meanings in Python depending on context- In expressions: multiplication (
*) and power (**) - In function calls: unpack iterables (
*list) and dicts (**dict) into arguments - In function definitions: collect extra args (
*args) and kwargs (**kwargs) - Bare
*in function signature forces keyword-only parameters - In assignments: extended unpacking (
first, *rest = iterable) - In literals: merge collections (
[*a, *b],{**d1, **d2})
Related reading
- What does axis in pandas mean?
- What does colon equal in Python mean?
- What does .contiguous do in PyTorch?
- What does double star/asterisk and star/asterisk do for parameters?
- What does enumerate mean?
- What does from __future__ import absolute_import actually do?
- What does functools.wraps do?
- What does functools.wraps do?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.