Retrieving from List of Lists

Screen Link:

My Code:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5]
row_5 = ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]

app_data_set=[row_1,row_2,row_3,row_4,row_5]
avg_rating=[app_data_set[0][-1]+app_data_set[1][-1]+app_data_set[2][-1]+app_data_set[3][-1]+app_data_set[4][-1]]/5


What I expected to happen:
I Expected the output to give the average rating

What actually happened:
it gave an error . It is because I use square bracket to open and close the avg_rating variable . However when i used the round brackets() to open and close the avg_rating variable , it worked perfecty . My question is that why is Round brackets () used here ? Why doesnt the code give the correct output with swuare brackets ?

Traceback (most recent call last):
  File "/tmp/7be57ecd1f189185485d6a1d3ee96c0976742f0d.py", line 86, in run_code_string
    exec(code, variables)
  File "<string>", line 8, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'int'

[] in Python usually either represent lists or are used for indexing/slicing.

When you use it as -

avg_rating=[ something ]/5

You are basically trying to ask Python to divide a list by an integer. That isn’t a valid operation hence the error.

() in Python usually either represent a tuple or is used as a way to simply “encapsulate” something (like you would in normal Math equations; helps with the order of operations).

When you have -

avg_rating=( something )/5

Python considers that as a mathematical operation (based on what () contains) where you are trying to divide that something by 5. So, no error.

2 Likes

Thanks a lot! This has solved my query