Clarification for the code

i get different outputs here -2,b and it’s confusing and seems difficult to understand positioning and list within list examples here.
could you please elaborate it in detail?

thanks

If you don’t understand how lists (as well as other object) are indexed, it would be better for you to stop nesting lists, until you do understand.

As I’ve already pointed out: a list object is indexed from 0 (zero) to the length of said object.

Like this:

position:    |  0  |  1 |  2  |
 data:       |  a  |  b |  c  |
- position:  | -3  | -2 | -1  |

Also (again), if you use letters as the data, rather than numbers, you’ll better able separate the data from the index numbers.

With my_list = ['a','b',-2] if you extract position 2 from that, you will get -2 as the result. If you then use that to extract position -2 you will get b as the result.

But, you’re using my_list = [3,1,-2] and as such position -2 is returning 1 which, as you can see, is in the same position as the b, in my first example.

this is clear, thanks but getting confused for my nested list example, could you please explain with respect to my example :-
it always returns 1 not sure why even if i use -1,-2,1,2 for print(my_list[my_list[2]])

my_list = [3,1,-2]

print(my_list[my_list[2]])

Thanks

my_list[2]-2
what you are doing…
my_list[my_list[2]]
results in…
my_list[-2]1
equivalent to
my_list[1]1

As Rob said.

1 Like

I’m still not sure WHY you insist on using numbers for your data:

my_list = ['a','b',-2]

print(my_list)

print(my_list[2])

print(my_list[-2]) # this is the last but one item in my_list

print(my_list[my_list[2]]) # this is my_list position 2, the last item in my_list (-2) 

To add. I don’t mean to come across as a d**K; it’s just frustrating when I offer advice and you don’t seem to act on what I’ve said.

There is nothing magical or special happening here. What you are seeing is 100% because of the numbers you have chosen to put in the list.

Remember that positions (indexes) in the list are numbered from zero onwards:

Index  0  1   2  
Value [3, 1, -2]

If you have a negative index, they count backwards from the end:

Index -3 -2  -1
Value [3, 1, -2]

When you do my_list[2], it looks at index 2, and returns the number -2. That number then gets used as the index:

my_list[my_list[2]]
--> my_list[-2]
--> 1

It might be easier for you to follow the code if you break it up and look at intermediate results:

# instead of this
print(my_list[my_list[2]])
# do this:
new_index = my_list[2]
print(new_index)
print(my_list[new_index])

Same for the other combinations you tried:

my_list[my_list[-2]]
--> my_list[1]
--> 1

my_list[my_list[1]]
--> my_list[1]
--> 1

my_list[my_list[-1]]
--> my_list[-2]
--> 1

If you had a bigger list, with different numbers, you would get different results. Or even if you just moved the numbers around. Try these:

my_list = [2, 0, 4, 1, 3]
print(my_list[my_list[0]])  # prints 4
print(my_list[my_list[1]])  # prints 2
print(my_list[my_list[2]])  # prints 3
print(my_list[my_list[3]])  # prints 0
print(my_list[my_list[3]])  # prints 1

On Sun, Aug 28, 2022 at 05:14:09PM +0000, v21 via Discussions on
Python.org wrote: