Understanding the code in the lesson "How Traffic Slowness Change"

In the “How Traffic Slowness Change” lesson (lesson 8 of 13) in the “Pandas Visualizations and Grid Charts” topic, the following code is included:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
traffic_per_day = {}
for i, day in zip(range(0, 135, 27), days):
    each_day_traffic = traffic[i:i+27]
    traffic_per_day[day] = each_day_traffic

The only real information given about this code is:

We use the zip() function above to iterate over range() and days at the same time.

The traffic_per_day variable is a Python dictionary. It contains five keys: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', and 'Friday'. For each key, we have a DataFrame containing only the data for that specific day. For instance, traffic_per_day['Monday'] has only the data for Monday:

In working on this post, I think I may have parsed the code and basically understand what it is doing (although not exactly why), but it is very unusual to have so little explanation for new topics as I don’t remember ever seeing the range function, let alone the zip function. If I am not supposed to worry too much about the details of how that was done, there is generally a note to that effect somewhere in the lesson and/or code. If I am supposed to understand this and be able to duplicate it, this was never taught, and I’m not sure if I ever would have been able to come up with it on my own.

Edit: Completing the lesson itself is very easy as we’re just creating a loop and the code in the loop can essentially be copied from what was taught in the lesson. I just make a habit of understanding the details of all code in the lessons unless I’m told that we’ll learn it later.

1 Like

Hey CuencaGuy, I think that’s a good suggestion for DataQuest to add some explanation, provide some external link to learn more about this function, or provide a note that you can expect to learn more about the zip() function at a later time. I often just google things like that as I come upon them or make an attempt to understand the Pandas documentation (or relevant library). Real Python is a website that has a nice explanation of zip() which I think is easy to follow.

Using the Python zip() Function for Parallel Iteration – Real Python

It is a pretty awesome function and will be used again later if you’re doing the Data Scientist learning pathway, when you get to data visualization (although there is still little-to-no explanation of it, if I recall correctly).

1 Like

Hi @CuencaGuy, @mkurylowski,

Could you please share your feedback about adding more explanations with the Content & Product teams of Dataquest? Just click the ? button in the upper-right corner of any screen of the Dataquest learning platform, select Share Feedback, fill in the form, and send it. Thanks!

Yes, I learned and understood some of what was happening through my internet searches as I usually google quite a lot of things as well. There was an included link, but it was just the documentation - something with which I’m not 100% comfortable yet. We had to use the functions again in a later lesson, so there was some practice with it as well. One of the big issues was the use of range(0, 135, 27). There are five days of data, each with 27 data points, so it makes some sense if you really try to parse it. To Dataquest’s credit, I don’t remember encountering code like this without a better explanation before.

I’ll check out the link you provided. Thanks.

1 Like