QA
Beginner LevelPython

How does extended iterable unpacking (first, *middle, last = items) work in assignment statements?

❓ Interview Prompt: Explain extended iterable unpacking (`*` operator in assignments), capturing remaining items into lists, and dictionary merging (`**`).

Python supports Extended Iterable Unpacking using the asterisk ('*') operator:

Assignment Unpacking:

Allows capturing multiple unpacked elements into a list during tuple/list destructuring:

•
'first, *middle, last = [10, 20, 30, 40, 50]' $\rightarrow$ 'first=10', 'middle=[20, 30, 40]', 'last=50'.
•
Dictionary Merging (''): '{dict1, **dict2}' merges key-value pairs into a new dictionary.

Code Implementation Example

python
numbers = [1, 2, 3, 4, 5]
first, *middle, last = numbers

print('First:', first)   # 1
print('Middle:', middle) # [2, 3, 4]
print('Last:', last)     # 5

# Merge dictionaries using ** unpacking
d1 = {'a': 1}
d2 = {'b': 2}
merged = {**d1, **d2} # {'a': 1, 'b': 2}
💡Unpacking iterables into variables and merging dictionaries.
🎯 Interview Key Takeaway

Use * in assignment statements to capture variable elements into lists cleanly.

#Unpacking#Asterisk#Destructuring#Beginner
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?