Is there a way to simplify the solution even more?

Screen Link:
https://app.dataquest.io/m/312/lists-and-for-loops/6/list-of-lists

The solution provided is:

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
print(avg_rating)

Is there a way to simplify this through something like the following:

avg_rating=(app_data_set[0:5][-1])/5

I expected this to sum up the last data point in rows_1 through row_5 and then divide by 5 to get the average rating. Of course, it didn’t work :frowning:

Additionally, as per several missions ago, is there a way to select the first and last two data points through code like row_1[0,3:4]?

Thank you,

Felix

Is there a way to simplify this

Not exactly in the way you are proposing. In the rest of the missions you will learn about for loops and you will be able to simplify the solution so that it’s not “hardcoded” in a way.

Later, you will learn some more advanced ways to easily sum across multiple lists. I can share some off that code, but it’s best that you slowly build on top of each “knowledge step”. If you’d rather get to see some of that code now, let me know.

But it’s great you are trying different things out!

is there a way to select the first and last two data points through code like row_1[0,3:4] ?

That depends on row_1. Or rather, what type of “container” it is for storing data. Till now, and I think for a few more missions, you will only be working with lists.

For lists, what you suggest is not possible. When attempting to extract an item -

You first use

row_1[0]

That will give you the first element (the value at the first index) in the list, row_1. That would be 'Facebook'.

And then, if you wish to extract something from that string, you can use slice operator -

row_1[0][3:4]

It’s a separate slice operation. And the above will give you an e.

If you wanted to select multiple non-consecutive items from row_1, like you ask about, that would require some more “advanced” methods that you will learn about later. There are some more advanced aspects to using the slice operator that will come up later as well.

And the kind of operation you are suggesting, row_1[0,3:4], that will come up later as well. But it would work for a different kind of data structure/container.

There’s a lot of “will come up later” in my answer. I suggest going through the missions at your pace, but if you still prefer to look at some of the code I can share that too.

1 Like

Thank you Doctor, I truly appreciate the in depth response to my questions. I guess I was jumping the gun a little bit, I noticed that 312-8 addresses my first question using ‘For Loop’ as you had mentioned.

1 Like

Since we are discussing about loops, why not try using a for loop to solve the problem.

  1. Initialize total = 0 and counter = 0
  2. for row in your list of lists, take the last column of row using row[-1]
  3. Increase total by row[-1] and counter by +1
  4. average_rating = total divided by counter

Cheers!