Can someone help me understand this code please? (if else)

for app in apps_data[1:]:
    price = float(app[4])
    # Complete code from here

    if price == 0:
        app.append('free')
    elif price > 0 and price < 20:
        app.append('affordable')
    elif price >= 20 and price < 50:
        app.append('expensive')
    elif price >= 50:
        app.append('very expensive')
        
apps_data[0].append('price_label')
print(apps_data[:6])

Its the last bit im confused by

apps_data[0].append('price_label')
print(apps_data[:6])

This looks like it will append ‘pricelable’ at the first coloum. But when you run the code its printed out last? as you can see below. and is the [:6] printing the first 6 headers? because the output shows many more.

Thanks

[['id', 'track_name', 'size_bytes', 'currency', 'price', 'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver', 'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num', 'lang.num', 'vpp_lic', 'price_label'], ['284882215', 'Facebook', '389879808', 'USD', '0.0', '2974676', '212', '3.5', '3.5', '95.0', '4+', 'Social Networking', '37', '1', '29', '1', 'free'], ['389801252', 'Instagram', '113954816', 'USD', '0.0', '2161558', '1289', '4.5', '4.0', '10.23', '12+', 'Photo & Video', '37', '0', '29', '1', 'free'], ['529479190', 'Clash of Clans', '116476928', 'USD', '0.0', '2130805', '579', '4.5', '4.5', '9.24.12', '9+', 'Games', '38', '5', '18', '1', 'free'], ['420009108', 'Temple Run', '65921024', 'USD', '0.0', '1724546', '3842', '4.5', '4.0', '1.6.2', '9+', 'Games', '40', '5', '1', '1', 'free'], ['284035177', 'Pandora - Music & Radio', '130242560', 'USD', '0.0', '1126879', '3594', '4.0', '4.5', '8.4.1', '12+', 'Music', '37', '4', '1', '1', 'free']]
1 Like

apps_data[0] is the first row, not the first member of the database.

So ‘price_label’ is appended to the end of the first row, which is the header row.

Append always adds the data to the end of a list.

3 Likes

Hi @Frankie

Just like @Johnsonk51502 stated, “ price_label’ is appended to the end of the first row, which is the header row.”

You are basically creating another column at the end of your list.

For example, pretend the current dataset has 16 items in each row. When you append to apps_data[0] you now have 17 items.

This is not technical language but “you can picture 17 items across a page kinda like 17 columns on a page or spreadsheet”.

You are using app.append to append a value to that 17th place you just created. As you loop through the dataset, you are using an if statement to determine what value to place as the new 17th item.

In the end, you basically created a new 17th item for each row.

To answer your next question:

**[:6] printing the first 6 headers? because the output shows many more.**

This is printing the first 6 rows starting at row 0 up to but not including row 6.

You can go here to read more about slicing a list

3 Likes

To add to the clear answers by @Johnsonk51502 and @Casandra_Hayward, you can also look into the structure of apps_data:

# Below is the whole app_data
# It is a list containing lists i.e. list of lists
[ # start of app_data list

# apps_data[0] -- the headers
['id', 'track_name', 'size_bytes', 'currency', 'price', 'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver', 'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num', 'lang.num', 'vpp_lic'],

# apps_data[1] - app 1 
['284882215', 'Facebook', '389879808', 'USD', '0.0', '2974676', '212', '3.5', '3.5', '95.0', '4+', 'Social Networking', '37', '1', '29', '1', 'free'],

# apps_data[2] - app 2 
['389801252', 'Instagram', '113954816', 'USD', '0.0', '2161558', '1289', '4.5', '4.0', '10.23', '12+', 'Photo & Video', '37', '0', '29', '1', 'free'], 

# apps_data[3] - app 3
['529479190', 'Clash of Clans', '116476928', 'USD', '0.0', '2130805', '579', '4.5', '4.5', '9.24.12', '9+', 'Games', '38', '5', '18', '1', 'free'], 

# apps_data[4] - app 4
['420009108', 'Temple Run', '65921024', 'USD', '0.0', '1724546', '3842', '4.5', '4.0', '1.6.2', '9+', 'Games', '40', '5', '1', '1', 'free'], 

# and so forth
...

# end of apps_data list]

The structure is row-by-row and not column-by-column.

To grab the first 6 headers, you’ll have to grab the header row and then grab the first 6 headers with list slicing.

# Take the header row only
headers = apps_data[0]

# Get the first 6 columns with list slicing
first_six = headers[:6] # equivalent to apps_data[0][:6]
2 Likes

Thank you for another detailed break down. This helps alot!

1 Like

Thank you. This makes sense now! Sometimes when you are staring at the code it just dosent click.

1 Like

Thanks for this. It does help looking at the table

1 Like