Project 1 (profitable app), Mission: 350, Screen:5, selecting name-header by app[0] and comparing

The code works as intended, but it should not. As previously the header was seperated from the android-list (of lists):

Removal of header, and designating it “android” for google data_set:

def open_data(file = 'AppleStore.csv', header = True):
    open_file = open(file, encoding = "utf8")
    from csv import reader
    py_read = reader(open_file)
    list_data = list(py_read) #reads it into list of lists 
    
    if header: #if header true 
        header = list_data[0]
        data = list_data[1:] #all the data excluding header
        
        return header, data
    else: 
        return list_data

header_roid, android = open_data('googleplaystore.csv')
header_io, io = open_data()

Comparing name throughout the rows
Thus following code should not access the headers, but instead the first name entry (Photo Editor & Candy Camera & Grid & ScrapBook) and compare it to the other data points.

for app in android:
    name = app[0] #the first list at 0'th element (but header is removed though?)
    if name == 'Instagram':
        print(app)

Outputs

['Instagram', 'SOCIAL', '4.5', '66577313', 'Varies with device', '1,000,000,000+', 'Free', '0', 'Teen', 'Social', 'July 31, 2018', 'Varies with device', 'Varies with device']
['Instagram', 'SOCIAL', '4.5', '66577446', 'Varies with device', '1,000,000,000+', 'Free', '0', 'Teen', 'Social', 'July 31, 2018', 'Varies with device', 'Varies with device']
['Instagram', 'SOCIAL', '4.5', '66577313', 'Varies with device', '1,000,000,000+', 'Free', '0', 'Teen', 'Social', 'July 31, 2018', 'Varies with device', 'Varies with device']
['Instagram', 'SOCIAL', '4.5', '66509917', 'Varies with device', '1,000,000,000+', 'Free', '0', 'Teen', 'Social', 'July 31, 2018', 'Varies with device', 'Varies with device']

Could someone explain why app[0] works?

Hi Chircory. Within the loop, app will correspond to a row at time from the android dataset. Since the header was removed, the first time the loop runs, app will be:

['Photo Editor & Candy Camera & Grid & ScrapBook', 'ART_AND_DESIGN', '4.1', '159', '19M', '10,000+', 'Free', '0', 'Everyone', 'Art & Design', 'January 7, 2018', '1.0.0', '4.0.3 and up']

and app[0] will be the first item in this list: 'Photo Editor & Candy Camera & Grid & ScrapBook'.

Since 'Photo Editor & Candy Camera & Grid & ScrapBook' != 'Instagram' and there’s nothing else to do, then the loop will go to the next row. This time app will be:

['Coloring book moana', 'ART_AND_DESIGN', '3.9', '967', '14M', '500,000+', 'Free', '0', 'Everyone', 'Art & Design;Pretend Play', 'January 15, 2018', '2.0.0', '4.0.3 and up']

and app[0] will be 'Coloring book moana'.

The loop will continue through the dataset, and each time app[0] will be different. When we finally get to a row where the first element is 'Instagram', then it will process print(app), which prints the entire current row of the loop.

I hope this helps, let me know if I didn’t quite get your question.

3 Likes

Thanks april.g, makes sense now.