App_data_set List and loops

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 = [‘Temple Run’, 0.0, ‘USD’, 1724546, 4.5]
row_5 = [‘Pandora - Music & Radio’, 0.0, ‘USD’, 1126879, 4.0]
app_data_set = [row_1, row_2, row_3, row_4, row_5]
for each_list in app_data_set:
#print( each_list)
print(each_list[0][-1], len(each_list))
Replace this line with your code


What I expected to happen:

I am just  a beginner so please bear with me. In the above code I deliberatelye included a line
 print(each_list[0][-1], len(each_list))
which is not required and I got this
k 5
m 5
s 5
n 5
o 5
Where we have 5s I was expecting 3.5, 4.5, 4.5, 4.5, 4.0, ie the last value in each list. I know we have not covered this. In simplest terms, what could i have done as a beginner.
I am grateful
What actually happened: 

Replace this line with the output/error


<!--Enter other details below: -->

When you print each_list[0][-1] you are asking python to print the last element, [-1], of the first element, [0], of the each_list list. The first element of this list, in the first case, is the string ‘Facebook’, and the last element of this string is the letter k.

Then you ask python to print the lenght of the each_list list. This list contains five elements and that’s why you are getting the number 5 every time.

If everything you need is the last item in the each_list list, all you need to do is this:

print(each_list[-1])

Let me know if it worked.

1 Like

Yes that worked thank you very much. What I was attempting to do was to get the last value by using the length of the list as an index. I know that [-1] gets the last item in the list but can I achieve the same result by using the length to specify an index or position. It was just an exercise/practice in using len(). I appreciate your help.

1 Like

If you want to use the length of the list as an index, you can run this:

print(each_list[len(each_list)])

But I recommend that you first assign the length of the list to a variable, like this:

length = len(each_list)
print(each_list[length])

Thank you so much. It worked. I know it is inelegant but Cheers

1 Like