Trying to understand the double index in python

Trying to understand the double index in python

The second index does the same as the first but with the nested value.
For exemple:

a = [(1, 2), (2, 3), (3, 4)]
a[-1]  # (3, 4)
a[-1][1]  # 4

In your example you dont have a list with numbers but enumerate objects converted to lists

sort(list(enumerate(nums)))

It means that you have data like this:

nums = [1, 2, 3, 4, 5]
enum_list = list(enumerate(nums))  # [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

It seems like left is an array containing tuples. I.e. Each element of the array is a tuple.

Ex: left=[(value1oftuple1,value2oftuple1),(value1ofarray2,value2ofarray2)]

In this case left[-1][1] would reference the first value in the last element of the array (value1ofarray2).

I found this by running your code and printing the value of left just before your code calls left[-1][1]. This way you can see what data type left is.

Trying to understand the double index in python

Leave a Reply

Your email address will not be published. Required fields are marked *