Question

so i have a tuple in python and i need to slice from the last element to first element but only want the last and first...

example:

big tuple: (1,2,3,4,5)

i want to slice the tuple so i end up with (5,1)

some_list[::len(some_list)-1]

above code slices from first to last and only includes the first and last..taken from here! so i need the exact opposite.

Was it helpful?

Solution

This code works:

some_list[-1:-len(some_list)-1:-(len(some_list) - 1)]

Another way:

some_list[::-len(some_list) + 1]

Of course you always can do

x[::len(x) - 1][::-1]

OTHER TIPS

Why not just do

new_list = (some_list[-1], some_list[0])

?

a = (1,2,3,4,5)    
print(a[0],a[-1])

Please try this. It has worked for me and seems to be a simple solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top