List for Loops Page no.10

Hey everyone. Hope everyone is doing good during Covid. So i am absolutely new to python. on LIst for Loops page 10.

apps_data=appsdata[1:]

This code is confusing me. on page 4 we learnt that [:3] this is first three consecutive and [-3:] last three consecutive data elements are selected but i dont know that the colon infront of the number represents? Please help

https://app.dataquest.io/m/312/lists-and-for-loops/10/the-average-app-rating

1 Like
appsdata[1:]

That representation is along similar lines to the ones you shared. For a simple example, a list -

a = [5, 6, 7, 8, 9]

As you know, the indexing starts at 0, so a[0] is equal to 5. And a[1] would be 6.

So, a[1:] would be all elements in the list a starting from index 1

That is, a[1:] -> 6, 7, 8, 9.

Similarly, a[2:] -> 7, 8, 9.

For any future queries like these, I would also highly recommend that you try to create a simple example like I did above with the list, and then try out that operation and print out the results. It often helps to be able to break it down as such and learn by exploring.

Slicing Python Lists


When we select the first n elements ( n stands for a number) from a list named apps_data , we can use the syntax shortcut apps_data[0:n] . In the example above, we needed to select the first three elements from the list apps_data , so we can use apps_data[0:3] or apps_data[:3]

  • apps_data[:n] when we want to select the first x elements.
  • a_list[-x:] when we want to select the last x elements.
1 Like

Hi @bb.fatma20:

Basically [1:] means you start from index 1 (the second element of the list) and select all the elements until the end of the list. In this case, this is a data preprocessing step used to remove the header of the csv file as this is redundant in calculating the sum of the ratings and thereafter deriving the average rating of the apps.

Hope this helps!

1 Like