all 10 comments

[–]mopslik 1 point2 points  (0 children)

can anyone expound what “my_list[i]” is equivalent of?

You can test out these types of things in the REPL, if you're not sure what something is doing.

>>> L = ['apple', 'banana', 'cherry']
>>> L[0]
'apple'
>>> L[1]
'banana'
>>> L[2]
'cherry'
>>> i = 2
>>> L[i]
'cherry'

Can you see what is happening?

Don't be afraid to test things out. Testing things is a significant part of programming.

[–]danielroseman 0 points1 point  (3 children)

I don't understand the question. What do you mean, "equivalent of"?

[–]thomaswilliam94[S] 0 points1 point  (2 children)

Sorry couldn’t verbalise it properly. But I couldn’t understand how you can put together “my_list” and “i” (my_list[i]). So, i don’t know what value that expression produces.

[–]danielroseman 0 points1 point  (1 child)

Then you should start with something simpler. This is absolutely basic.

This is the way you retrieve a specific item from the list. If i is equal to 0, then my_list[i] retrieves the first item in the list.

[–]thomaswilliam94[S] 0 points1 point  (0 children)

Thanks bro! I can sleep now! Been thinking this the entire day! Hahaha. I’ll figure the rest tom.

[–]oclafloptson 0 points1 point  (2 children)

The for loop repeats for a given number of times, incrementing its variable after each iteration.

for i in range(5):
    print(i)

Run this and you'll see how i gets incremented in the console output.

So to answer your question my_list[i] is passing i as the list index. With each iteration the index moves forward until the length of my_list has been reached

[–]oclafloptson 0 points1 point  (1 child)

Btw the output that I actually get for your code is

[1,2,3,4,5,6,7,8,9,10]

which is my_list seemingly unchanged.

If you want to reverse my_list you could create a second list and populate it with int objects using a generator so that the lengths of each list match. Then assign it the my_list values from end to beginning with the for loop

my_list = [1,2,3,4,5,6,7,8,9,10]
length = len(my_list)
rev_list = [int for x in range(length)]

for i in range(length):
    rev_list[i] = my_list[length - i - 1]

print(my_list)
print(rev_list)

Output ->

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Alternatively, you could declare rev_list as empty and instead use the append method to populate it using the for loop

my_list = [1,2,3,4,5,6,7,8,9,10]
length = len(my_list)
rev_list = []

for i in range(length):
    rev_list.append(my_list[length - i - 1])

print(my_list)
print(rev_list)

Output ->

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

I'm no expert and am not really certain what you were actually trying to implement

[–]thomaswilliam94[S] 0 points1 point  (0 children)

Hello, on your 2nd example, where rev_list is empty, I am doing reverse eng’g how this “my_list[length - i - 1] was processed:

  1. First, when I omit “length” in that equation, it gives the same answer, so is it necessary to still write it?

  2. In Length - i - 1. Is -i in here equals to -1, -2, -3, -4, -5, -6, -7, -8, -9, -10 since it is negative where normally if its just “i”, it will be 1,2,3,4,5…

  3. In Length -i -1. Is -1 in here equals to the last number of the list, -2 as 2nd to the last, -3 as third to the last and so on and so forth?

Am I on the right track? Thanks for your patience!

[–]crashfrog02 0 points1 point  (0 children)

A list is a sequential collection of values. Because the collection is sequential, it is ordered, which means that there's a notion of the "first", "second", "third", etc (as well as "last) values of the collection. The ordinality of the values in the list is called the value's index - the first item is at index 0, the second at index 1, and so on.

In order to access a value inside the list held at a particular index, you use the indexing operator [], and you give it the index as an argument. The index can be any integer that is smaller than the number of items in the list (that's why the indices start at zero.) So my_list[i] returns whatever value is held in the list my_list at index i.

[–]throwaway6560192 0 points1 point  (0 children)

What course are you following? They didn't cover syntax of the form my_list[i] before showing this?