0
Have fun with sliced lists: how to get an inverted sublist when using negative indices
I was playing around with slices and negative indexes. If you need an inverted sublist using negative indice, it is best to use a temporary variable to save your sublist before you are going to invert it: squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[-5:-3]) print(squares[-5:-3:-1]) print(squares[-3:-5]) print(squares[-3:-5:-1]) print(squares[-4:-6:-1]) print(squares[5:7]) print(squares[5:7:-1]) sublist = squares[-5:-3] print(sublist) print(sublist[::-1]) Line 3 should intuitively lead to the same result as the last line, but it doesn’t.
6 Respuestas
+ 1
On line 3 You are saying to go from -5 to -3 in backward (-1)
Which isn't possible
So it returns empty list [ ]
You should have passed [-3:-5:-1]
Hope It Helps You 😊
+ 1
if you are talking of the following line
print(squares[-5:-3:-1]) it returns empty list since you can't have a negative step when you are moving from left to right
in squares[-5:-3] the default step is 1 ,it could be re written as squares[-5:-3:1] now it will work fine and slice the values from index -5 to just before -3 i.e. 25,36
+ 1
CMW Okay
So you want output [36,25]
36 is at -4 (From where our slice will start)
25 is at -5 (From where our slice will end so we'll use -6 as last num is not included)
And we want to go backward so step would be -1
>>>squares[-4:-6:-1]
[36,25]
So whenever wanted to get inverted sublist just pass
•Index from where to start
•Then the index where to end -1(+1 if not wanted inverted)
•And a step of -1
Hope It Helps You 😊
0
Maybe I should have been clearer: the idea was to get the inverse of a sublist, using negative indices. The desired result was [36, 25], the inverse of what you suggest, Abhay.
My above code produces the following output:
[25, 36]
[]
[]
[49, 36]
[36, 25]
[25, 36]
[]
[36, 25]
[25, 36]
[36, 25]
@Hacker Badshah: the fourth line is the output of what you suggested ([49, 36]), but it is not the correct solution to the problem.
0
Thanks Hacker Badshah.
I realized that (see line 6 of my code). But I think it is neither an intuitive nor a very elegant solution. Suppose you want to use both the original sublist and its inverse in a piece of code, maybe even with the indices coming out from some calculation:
1. Fine, if you use positive indices:
squares[5:7] #sublist 1
squares[5:7:-1] #inverse of sublist 1
2. But the same logic does not work with negative indices
squares[-5:-3] #sublist 2
squares[-5:-3:-1] #is not the inverse of sublist 2
sublist3 = squares[-5:-3]
sublist3[::-1] #this is the inverse of sublist2
So, instead of tweaking the indices when they are negative and you need the inverse, I think it is more elegant to use an intermediary variable. That way you don't need to think about adjusting indices to get the desired result.
Hope I was clear and it helps 🙂
0
Just realized that there was an error in my statement above:
squares[5:7:-1] actually does not work (ouput is an empty list [])
So, the solution would always be to use an intermediary variable, or to use the shortened form of immediately applying the inversion to the result from the slicing:
squares[-5:-3][::-1] # This seems to be the safest way without doing any acrobatics on the index range, in particular if the index values are unknown at runtime
print(squares[5:7])
print(squares[5:7][::-1])
print(squares[-5:-3])
print(squares[-5:-3][::-1])