Introduction to Python | Conditional Statements| Screen 1

Screen Link:
https://app.dataquest.io/c/57/m/313/conditional-statements/1/if-statements

My Code:

for row in apps_data[1:]:
    rating = float(row[7])

What I expected to happen:

What actually happened:


  • Extract the rating and convert it to a float using float(row[7]). The rating has the index number 7 and comes as a string, so we need to convert it to a float.

I did not understand this explanation. Price is in column 4 and rating is in column 7. But it is mentioned row.

And it is a numeric value only, why should we convert it into float?

Indeed the rating is in the 7th column (actually 8th column since the column index starts at 0).
As you can see the data is a 2-dimensional array

If you want to access the whole rating column, you need to select it line by line and then select the 8th column, Since the for loop will go through each line, you have to pick the 8th item of each row.

The 1st item in the apps_data[1:] list is the first row of the whole data, similarly the 2nd item in the apps_data[1:] is the 2nd row.
Now to access the rating from each row you select the 8th item in that row (which is also a list), hence row[7]

Remember row is just a name, you could name it something else and it would still work.

Hope this helps

1 Like

hi, just had a quick question and I thought asking you would be faster than just putting it out there. I am on the same topic as @ashwin.mvsr, this is the whole code

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

apps_names =
for row in apps_data[1:]:
# Complete the code from here
name=row[1]
apps_names.append(name)
print(apps_names[:5])

I just don’t understand the index value [1:] in the “for row in apps_data[1:]:” because the question or the task is to print the first 5 elements name and they have the index value of [1] as shown in the “name=row[1]” I just don’t get the point of the index value of one in “for row in apps_data[1:]:”

Thank you, hope to hear back from you

Hi @Joyeux , welcome to DQ community.

This topic introduces you to list of lists. In list of lists, a dataset is stored as list of list as the name suggests. Below given is a simple example

[   [ Number, Name, Type],
     [01, Apple, Fruit],
     [02, Cat, Animal],
    [03, Orange, Fruit]
]

Here you can see there are three lists within a big list.

In order to access each value, we first need to select the each row of the big list and then go into each items of the inside list.

To select each row of the big list, we use for loop. But then you also have noticed that the first row in this example is [ Number, Name, Type] which is more like the heading of the list, not the content of the list. Hence we have to exclude this row while accessing the items in the list. Hence we select everything other than what is on the first row ( at index 0) which you can represent as big_list[1:]

In your example it is apps_data[1:]

Since we have selected the big list without the header, now what we have to do is go through each row using for loop.
for row in apps_data[1:]:
That happens in this step.

Once we have each row, we need to pick what we are looking for. In this case, the name of the app which is at 2nd column (ie at index 1). Hence we call that index in each row of the big list. name=row[1]

We then create an empty list called apps_names and append all the names into that list. Now since we wanted only the 5 names, we will print 5 rows of this new 1 dimensional list. print(apps_names[:5])

I hope this clarifies your doubt.

Also please keep these points in mind next time when you ask a technical question in the community.

Hi, @jithins123 I just want to say thank you, the confusion was with the index values. But I still have a little confusion you said that the at index zero was represented by big_list[1:] which excluded the first row with the index of zero. but in my case when I tried apps_data[0] instead of the apps_data[1:] I got different answers, what’s up with that shouldn’t I have gotten the same result

Hi @Joyeux

In the example I gave

big_list = [   [ Number, Name, Type],
     [01, Apple, Fruit],
     [02, Cat, Animal],
    [03, Orange, Fruit]
]

big_list[0] would be [ Number, Name, Type],
and big_list[1] would be [01, Apple, Fruit]

In Python the index starts from 0. So if there are 3 items in a list the first one will be at 0 and the last one at 2. I hope this is clear.

So now if you want to access an item in the inner list ( of the list of list) you first have to select one of the smaller list.

If you want to select the column name from the header, you will have to select the header row first. In the above example it is at 0 index and can be accessed by big_list[0]

We can assign it to header = big_list[0]

Now that we have the header, we can access its items as header[0] which will return the value Number

(It is also same as big_list[0][0] )

Well now we don’t want the header. We want the data without header. So we take the big_list and call everything but the header. It can be done by big_list[1:]. This will return the list without the first row, in this case the header.

     [01, Apple, Fruit],
     [02, Cat, Animal],
    [03, Orange, Fruit]

You can apply the same logic to the app store data.

Was this clear?