Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Python Tip: Extract First, Last, and Middle Elements in One Line with Star Unpacking

CodersAura 1 min read

A handy Python trick called extended iterable unpacking lets you extract the first, last, and middle elements of a list in a single line—no indexing required.

Simply use the star operator (*) to capture the middle items:

first, *middle, last = [10, 20, 30, 40]

After this assignment:

  • first becomes 10
  • middle becomes [20, 30]
  • last becomes 40

This technique, introduced in Python 3, helps write cleaner, more readable code. It’s especially useful for beginners learning Python programming. For more Python tips, follow the CodersAura channel.

React