Guided project [1]: Prison Break

Prison Break Project .ipynb (79.5 KB)

Hi,
This is my first guided project and I had a lot of fun.
I have no coding background, and have a couple questions doing this project.

  1. In the step where you modify data so that the last element of each row (the Details) isn’t present anymore. I wanted to understand the code better, so I did:

index = 0
for row in data:
data[index] = row[:-1]

expecting the return would be just the first row without the last element and the other rows would remain the same, but instead I got a weird result, where the list began in 2020, then jump back to 1973.

  1. In the step for min and max year, I have trouble understanding
    min(data, key=lambda x: x[0])[0]

my understanding for min() is only limited to:
x = min(1,2,3)
print(x)
then it would return 1

having that said, I have trouble understanding why you could put data first then followed up by key=lambda x: x[0])[0], and I couldn’t understand what key=lambda x: x[0])[0] really is either.

I would very much appreciate if someone could answer these questions. Thank you for spending time on this post. Hope to see you all soon in the next project.

Click here to view the jupyter notebook file in a new tab

Hi @cy.ericson

Welcome to DQ community. Glad you took the first step to learning data analysis using python without prior coding experience. It is a good first step, especially when you’re having fun with guided projects!

To answer your first question

index = 0
for row in data:
    data[index] = row[:-1]

if you have tried this code, you can see that index is set to zero and inside the for loop it is not getting updated. So whatever iteration is happening to row , the other side remains data[0].

Was that what you wanted to achieve?
Anyway if you want clarity while handling for loops and what it returns, it is a good practice to try and print each element of the loop to figure out what exactly is happening inside the loop.

For example you can try to print row or row[:-1], data[index] etc and decide your next steps.
I hope this will help you to figure out the issue you had.

Now coming to the second question, it is related to lambda function. Going into the details of lambda function was probably out of scope of the first course of python, hence it wasn’t mentioned in that chapter. But you will learn about it in one of the upcoming lessons.

In case you want to know more about it now, you can check article like this one to get familiar with it
https://towardsdatascience.com/lambda-functions-with-practical-examples-in-python-45934f3653a8

You’d have also imported a few things from the helper function at the beginning. They are also slightly advanced concepts which you’ll learn in future lessons. You’ll understand it better when you get there

1 Like