Slicing the CSV Rows

How do you slice the lists in the “Loops And For Loops 10. Reading CSV files” exercise?

from csv import reader

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

print(apps_data[1:3])

OUTPUT:
[[‘284882215’, ‘Facebook’, ‘389879808’, ‘USD’, ‘0.0’, ‘2974676’, ‘212’, ‘3.5’, ‘3.5’, ‘95.0’, ‘4+’, ‘Social Networking’, ‘37’, ‘1’, ‘29’, ‘1’], [‘389801252’, ‘Instagram’, ‘113954816’, ‘USD’, ‘0.0’, ‘2161558’, ‘1289’, ‘4.5’, ‘4.0’, ‘10.23’, ‘12+’, ‘Photo & Video’, ‘37’, ‘0’, ‘29’, ‘1’]]

Hey Donnie!

This type of data structure is a list of lists — you would want to either loop through each element in the outer list (which happens to be a list itself) and pull the relevant information out that way, or you can index the outer list twice. For example, running apps_data[0][1] would first locate the element at apps_data[0] which is this list:

[‘284882215’, ‘Facebook’, ‘389879808’, ‘USD’, ‘0.0’, ‘2974676’, ‘212’, ‘3.5’, ‘3.5’, ‘95.0’, ‘4+’, ‘Social Networking’, ‘37’, ‘1’, ‘29’, ‘1’]

We then index that result by [1] which pulls the second element of the result above: Facebook

Does that make sense?

Donnie, you can slice the list of lists by executing apps_data[0][1:3] to return the second and third element of the first list. I hope this helps!