# Unpacking Tuples

Python has a neat feature called "Unpacking". You can assign a list to multiple variables at once!

So, you already know how to do this:

x = 10

But, you can also do this:


# Unpacking
x, y = 10, 20

# Same thing, but boring
x = 10
y = 20

Unpacking involves something called a tuple. A tuple is like a list, but unlike a list, you can't change it once it is created. Tuples are created by putting commas between things, and you can put parentheses around it to make it more clear. So these are all tuples:

You can assign one tuple to another, but (mostly) they have to be the same size. ( they can be different sizes but we'll save that for later. )

But the code below would be an error, because on the left side, the tuple has three elements, but there are only two on the left.

Now that you know about tuples, you will see them in a lot of places in Python programs.

# Zip and Enumerate

The really important part about tuples is when they are used in two important functions, enumerate() and zip()

Enumerate means "to count out, one by one", and the enumerate() function will count things for us. Here is how we use it:

Hmmm .. it looks like it printed out tuples! In each tuple, the first element is the number, and the second is the item. But, we know that we can unpack tuples, so we could also write:

Enumerate is a very powerful way to count the things that we are iterating over.

The zip() function is another very important function. Zip takes one or more iterables, like strings, lists and tuples, and returns a tupe with the first one from each, then the second, then the third.

As you can see, the first tuple is the first item from each of the iterables. The second tuple is the second item from all of the tuples, etc.

# A Zippy Program

Write a program that has three lists or tuples that have commands for your turtle.

  • One list has 10 distances to move ( forward from our previous programs).
  • One list has the direction to turn ( called left in past programs )
  • One list has colors for the turle's pen

Zip all of the lists together, then unpack them in a loop, and call a function with the values that makes your turtle change color, turn, and move.

Click me for a hint

turtle = ...

moves = [ ... ]
turns = [ ... ]
colors = [ ... ]


def move_turtle(..., ..., ...):
    turtle.color(...)
    turtle.forward(...)
    turtle.turn(...)


for move, turn, color in zip(...):
    ...