Editor

Head and Tail with Star

Intermediate · Write the code · from Python Unpacking: Starred Expressions, Swapping, and Destructuring

From the lesson: Python Unpacking: Starred Expressions, Swapping, and Destructuring

Imagine you order a package online that contains three items: a book, headphones, and a phone case. When the box arrives, you open it and put each item where it belongs. The book goes on the shelf, headphones go on the desk, and the case goes on your phone.

Read the lesson

Use a starred expression to unpack the numbers list into head (the first item) and tail (everything else). Print both on separate lines.

numbers = [10, 20, 30, 40, 50]
# Unpack into head and tail using *

More Unpacking exercises

All Python exercises · 50 practice problems · Data Structures quiz