Pregunta

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.

¿Fue útil?

Solución

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]

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top