Prision Break Guided Project - 7 of 13

Hi Community!

Could someone please help me to understand what does it represent in ’ data[index] '?
I understand that ‘index=0’ is written for loop purpose, and ‘data[index]’ should be a syntax for variable name?

url = "https://en.wikipedia.org/wiki/List_of_helicopter_prison_escapes"
data = data_from_url(url)

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

Thank you in advance!

1 Like

Hi @pearly.ho,

data[index] is simply the specific row/element in data.

data[0] will be the first row, data[1] the second row, and so forth.

The loop starts with index 0, accesses the first row in data with data[0], and assigns the value in data[0] with the value from row[:-1]. Essentially, it is assigning its own value but the last element in the row is removed. That pattern repeats with a constantly incrementing index until it loops over all of data

data is the variable with the type of list. And list can be indexed with [index]. Specifically data is a list containing multiple lists, so each index of data will return a list as well.

A flat/one-dimensional list is something like this: [0, 1, 2, 3] which is a simple list of integers.

But data is the two-dimensional kind e.g. [ [0,1], [2,3], [4,5] ]. In the example I gave, that is a list containing lists of integers. For data, it is a list with multiples lists of strings.

3 Likes

Hi @wanzulfikri ,

Thank you for providing the explanation. It greatly helps!

Have a great week ahead :grinning:

3 Likes

No worries, @pearly.ho.

If you’re interested in simplifying the loop further, you can also write it like this:

for index, row in enumerate(data):
    data[index] = row[:-1]

enumerate will give both the index and the row so you don’t need to keep track of the index manually and increment the index every time.

Anyhow, you have a great week ahead too.

2 Likes