Python Forum

Full Version: negative to positive slices
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i can sequence single character indexes like
Output:
>>> a='abcdefghijklmnopqrstuvwxyz' >>> [a[x] for x in range(-6,6)] ['u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f'] >>> a[-6:6] '' >>>
but a slice doesn't work anywhere near that way. so, i have to do
Output:
>>> ''.join(a[x] for x in range(-6,6)) 'uvwxyzabcdef' >>>
is there a more direct way using a slice to get this?
Did you try a[-6:]+a[:6] ?
In [1]: a='abcdefghijklmnopqrstuvwxyz'

In [2]: a[-6:6:-1]
Out[2]: 'utsrqponmlkjih'
wavic: i want to go in the forward direction, not backwards.