Flat_Eric Contemplating Data Type Conversions

List Unpacking

List Unpacking

The last thing we need to learn about lists is List Unpacking.

Imagine we have a list: nums = [0, 1, 2, 3]. We can "unpack" that list by assigning it to comma-separated variables in one go:

  1. Print a, what do you see?
  2. Now try b, c and d.

The Asterisk (*) Catch-All

Check this out: if you have a long list but only a few variables, you can use the * operator to capture the "rest."

The Takeaway

You might be thinking: "When will I ever need to use this in my programming life?" Like many things in coding, you don't need to memorize every detail. You just need to understand the concept. When you're faced with a problem in the future, you'll recall that a solution exists and you can look up the syntax. Knowing that List Unpacking is a tool in your kit is enough for now.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: The Podium Finish

The race results are in, and we need to separate the medalists from the rest of the pack.

Exercise 2: Sensor Stripping

We received a data packet where the first item is a date, the last is a location, and everything in between is a temperature reading.

Exercise 3: Professional Ignoring

We only need the username from a profile list, and we want to ignore the sensitive data that follows.

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course