Stuck on slicing data in Prison Break Project

Hey everyone,

I have this slicing problem since 2 days ago and I have tried to look for solution about this matter too since 2 days ago. It keeps giving me Typeerror and I have tried many things (star from beginning, run all cells, copy other people solution, go back to the theories) and didnt give me the result I wanted, which is this one : ([‘August 19, 1971’, ‘Santa Martha Acatitla’, ‘Mexico’, ‘Yes’, ‘Joel David Kaplan Carlos Antonio Contreras Castro’])

I am confused how to use the increment in this matter. Why do I have to use it if I only want to have one list (without the last element) from many list?

What should I do? :frowning:



1 Like

For indexing, for example data, you’ll need to use integer in square brackets like so data[0].

Here’s what’s happening in your code:

index = [0] # [0] is not the 0 integer; it is a list containing one 0 integer

for row in data:
    # what's happening here is data[index] is equivalent to data[[0]] and not data[0]
    # hence, the error you're getting
    data[index] = row[:5]
    # this can be problematic as well; it is equivalent to index = [0] + 1 and not index = 0 + 1
    index += 1

Can you clarify this? I’m quite bad at reading things.

The for loop that you have loops over all list within data.

In the first iteration, the loop assigns the first list in data to row. It then takes the first five elements within row and assign it to data[0]. This means that if you have six elements inside row, it will ignore the sixth element which is at the same time the last element. Once that’s done, index is incremented and the loop starts the second iteration which assigns the second list in data to row.

You don’t need the loop if you only want one list from data. The for loop is only needed to transform all lists within data from a list with six elements to a list with only five elements. The list with only five elements has everything except the Details info.

I’m not sure if I explained things properly; feel free to ask questions or if I made mistakes somewhere.

1 Like

Hi wan,

thank you for your explanation!

But I am still confused on how to write it right because I already followed the instruction and even copied the solution from other people.
And I feel like I understand but I dont know how to start (thats my problem! :smiling_face_with_tear:)

I even still confused with which one is iteration and iterable! :pensive:

N.B : I am myself not an english native speaker so when take this course I try my hardest to understand.

1 Like

Ah, no worries.

N.B : I am myself not an english native speaker so when take this course I try my hardest to understand.

That’s alright. I’m not a native speaker myself, so I understand the struggle.

Do you know how to download your notebook? You can share it in your post and I can take a look.

I think I just donwloaded it now, lets see if it works
Guided Project_ Prison Break.tar (19 KB)

Like that?

Yup, that should work.

Uploading the .ipynb file is also an alternative way that works well with this forum.
Basics.ipynb (9.1 KB)

Anyway, I’ll have a look. I’m quite slow so feel free to do something else.

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

1 Like

with what should I open the document ? (sorry for stupid question)

I will try to look at my code again, maybe also review the theory and thank you!

It seems to be a simple fix.

index = [0] # it should be index = 0

for row in data:
    data[-1] = row[:5]
    index += 1

Just change [0] to 0.

Before this, when it sees index += 1, it actually thinks you want to combine [0] with 1 which is not possible. What you really need is to add 0 to 1 for example, which is the addition of two numbers together.

The [0], is a list that we can iterate over for example in a for loop. That’s one reason it’s also called an iterable (aka iterate-able). A list can combine with another list e.g. [0] + [1] = [0, 1].

Have you downloaded Anaconda? It includes the Jupyter Notebook which you can access with Anaconda Navigator.

Yes I do have anaconda!

and I have fixed the index = [0] to index = 0 , but it still gives me unwanted result. Now it gave me :
; [‘Augus’, ‘Santa’, ‘Mexic’] :rofl:

I get more confused with my code now tho.

Why is it data [-1]? If I do it like this but with row[:-1] it gave me [‘Ye’, ‘Santa’, ‘Mexic’]

1 Like

Haha that’s alright. Debugging can be confusing.

What code did you use to print this?

data[-1] refers to the last element in data, but row[:-1] refers to all elements in row except the last element.

One thing to note is data is not the same as row.

And in your code, I think data[-1] should be data[index]

1 Like

I am seriously crying right now and doubting myself :sob: Am I that stupid.

Guided Project_ Prison Break (1).tar (15.5 KB)

Here, take a look. If I change data[-1] to data[index] back, it gave me Typeerror. And I should write print(row[2]) for first 2 rows if I want to, right?

I’m very sorry to hear that.

I’m here to help if you’re willing to work through it.

I changed it to index and it looks fine on my end. Maybe you need to click the “restart & run all” option in the kernel tab.

Trying to help.ipynb (5.8 KB)

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

If it’s row[2], it’ll print the third element because index starts from 0. To print the first two rows, you’ll need row[:2]. The slice is needed there.

1 Like

I run them and it worked (at least it doesnt give me an error)!

But I print(row[:2]) and it gave me [‘September 25, 2020’, ‘Forest prison, Brussels’] :face_with_raised_eyebrow:

The giant list is hard to understand where it stops :nauseated_face:

1 Like

I see.

Yeah, that’s the correct output.

Oh my mistake for not clarifying early on. Your whole data is inside data actually. So, to get first two rows from your whole data, you’ll have to use data[:2].

Row is actually a single row or list from your whole data. It doesn’t have its own rows (in a tabular sense). What it has is elements.

Each row looks like the following: '[August 19, 1971', 'Santa Martha Acatitla', 'Mexico', 'Yes', 'Joel David Kaplan Carlos Antonio Contreras Castro'].

So if you use row[:2], it’ll return ['August 19, 1971', 'Santa Martha Acatitla'].

If the list is too hard to understand, you can just slice data. Maybe data[:5] like in the example.

■■■ IT WORKED!!!

Thank you wan!!! I still have a problem of understanding my code but Imma write down your explanation on the paper :grin:

1 Like

That’s great to hear.

Feel free to PM me or post a question in the forum if you need further help. There are others here in the forum who are much much better than I am and can provide better guidance.

Anyhow, good luck and keep on learning.