1

Question

I have a question about Python creating new variables derived from other variables. I am struggling to understand how Python automatically knows how to generate variables even when I do not explicitly tell it to.

Details

I am a new Python user, and am following along in the tutorials in: Joel Grus, "Data Science From Scratch".

In the tutorial, I create three list variables:

  1. friends contains the number of friends that someone has on a given social networking site

  2. minutes refers to the number of minutes that they spend on the site

  3. labels is simply an alphabetic label for each user.

Part of the tutorial is graphically plotting labels next to the points when I create a scatterplot. In doing so, Python seems to automatically generate three new variables: label, friend_count, and minute_count.

In short - how? How does Python know to create these variables? And what do they do? They do not correspond to the mean, median, or mode of any of the lists.

Code

import matplotlib.pyplot as plt
from collections import Counter

def make_chart_scatter_plot(plt):

    friends = [ 70, 65, 72, 63, 71, 64, 60, 64, 67]
    minutes = [175, 170, 205, 120, 220, 130, 105, 145, 190]
    labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

    plt.scatter(friends, minutes)

    # label each point
    for label, friend_count, minute_count in zip(labels, friends, minutes):
        plt.annotate(label,
                 xy=(friend_count, minute_count),
                 xytext=(5, -5), # but slightly offset
                 textcoords='offset points')

    plt.title("Daily Minutes vs. Number of Friends")
    plt.xlabel("# of friends")
    plt.ylabel("daily minutes spent on the site")
    plt.show()

Thank you!

4
  • 1
    The 3 names are assigned the results of zip in the for loop Jul 14, 2016 at 14:43
  • 1
    friend and the others are just names that we give python to refer to the object created on the right. Python doesn't make it up, we tell it to use those names.
    – cdarke
    Jul 14, 2016 at 14:44
  • 1
    wiki.python.org/moin/ForLoop
    – Jeremy
    Jul 14, 2016 at 14:45
  • Thank you, everyone - I appreciate your quick responses. It sounds like I have a lot to learn about for and zip.
    – Yasha
    Jul 14, 2016 at 14:50

1 Answer 1

4

So you're actually creating the variables in the for loop:

for label, friend_count, minute_count in zip(labels, friends, minutes):

When you zip those together you're grouping them by the index, so the first item it iterates to is (70, 175, 'a'), the second is (65, 175, 'b'), and so on. Python then unpacks those three results, because you ask it to assign to three variables, label, friend_count and minute_count. If you were trying to unpack four variables and only supplied three names, for example, it would raise an error.

Then each time it iterates through the loop it reassigns the next values to those three variables.

Another way to think about this: if you wrote that line as:

for values in zip(labels, friends, minutes): 

then values would just be the three items together every time, and those variables would not exist. You could then unpack them within the loop if you wanted. The way you posted is just a neater way to do it.

One more example of unpacking that you can play with yourself:

x = [1, 2, 3, 4]
a, b, c, d = x

would assign a=1, b=2 and so on. However:

a, b = x

returns an error:

ValueError Traceback (most recent call last) in () ----> 1 a, b = x

ValueError: too many values to unpack (expected 2)

This gets more interesting using the * operator:

a, *b = x

results in:

In [38]: a
Out[38]: 1

In [39]: b
Out[39]: [2, 3, 4]

That is, the * tells Python that the last value is the place to dump whatever is left. This behavior is again used a lot in functions, but can be used in for loops as well. Actually note that this * operator only works with lists, as I illustrated above, in Python 3.x. In 2.x you can still use it in functions this way, but not in assignment.

5
  • Wow, thank you for such a prompt response! So, just to make sure I understand, the code is telling Python to unpack the zip into three containers that I have specified? Not trying to be dense, just want to make sure I get it :)
    – Yasha
    Jul 14, 2016 at 14:49
  • Yes, this is actually very standard in Python, and comes up for example when returning values from functions a lot. I'll add another example to make it clear.
    – Jeff
    Jul 14, 2016 at 14:50
  • Thank you for the example. I am able to follow along right until I try to use a, *b = x, which for some reason gives me "SyntaxError: invalid syntax".
    – Yasha
    Jul 14, 2016 at 14:58
  • You're probably in Python 2.x then, it's a newer feature.
    – Jeff
    Jul 14, 2016 at 15:02
  • Ah - okay! Yes, I am using Python 2.7. Thank you very much for your help with everything!
    – Yasha
    Jul 14, 2016 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.