I have got a harmless list "mylist" of six items. Never empty, never any beastly items.
mylist = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
I found that there are two ways to index a list for slicing purposes. For this question I shall call them upper index (from 0 to 5) and lower index (from -1 to -6). Here is a quick diagram (I had posted a photo with colors, which was replaced by another user):
<<<<|_________mylist[5:?:-1]_____________|
|___mylist[0:3:1]______|>>>>>>
?
(x) 0 1 2 3 4 5 (6)
aaa bbb ccc ddd eee fff
(-7) -6 -5 -4 -3 -2 -1 (y)
?
<<<<<<|___mylist[-1:-5:-1]___|
|______mylist[-6:?:1]________________|>>>>>
For easily slicing at the start of my list, I can use the upper index like this:
>>> mylist[0:3:1]
['aaa', 'bbb', 'ccc']
For easily slicing near the end, I find the lower index helpful, like:
>>> mylist[-1:-5:-1]
['fff', 'eee', 'ddd', 'ccc']
By "easily" I mean for example the aspect of not concerning my code with the length of the list.
I learnt that Python slicing is "always holding back" (also known as "up to but not including"). I am showing this in my diagram as the "arrow-heads" pointing from the end of a slice to the "next item" ahead ("ahead" in the sense/direction of the slicing).
For my users I need to show the first so many items from my list. (Those are the results from a recursive search and evaluate and sort run.) Because normally this list is rather long, I do print it backwards (in the console), so that when the script ends, the best n
results will still be visible on the screen. So my step is -1
in this context.
I would like to use variables, to allow for different needs for different users (and different screen sizes). For debugging and for certain users I want to be able to print all results, i.e. the entire list backwards. I would like something in this syntax:
start = 5 # I only need to know "5" for debugging, normally I can use
# any small number to show a few selected items of mylist
end = x # this is my main question, see photo
mylist[start:end:-1]
My question is please, how do I write the first(topmost) slice in the diagram, by using variables and by using the upper list of indices? Or in other words, what is the numerical value of x
(and y
) in the diagram?
Semi-solutions that I do not want to use:
mylist[5::-1] # no variable at all
mylist[5:None:-1] # does not fit in my normal function for printing
I have not managed to use None
in any simple operations with my variables, it gives me errors like:
end = None - 20
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
And I have not managed to convert None
to any integer or other number. Is there a way? Would probably be a sacrilege in Pythonland...
If I cannot find the secret number of x
, then I might need to work with this:
mylist[-1:-7:-1] # I want to use the upper index rather
But this approach involves checking the length of mystring
.
end = -1 * (len(mystring)+1) # extra-explicit as an example
returns -7
mystring[-1:end:-1]
returns ['fff', 'eee', 'ddd', 'ccc', 'bbb', 'aaa'] # this is the output I want, just not the method
What I have done before asking you:
I have written a testing-script and have tried guessing x
. I have searched and read up plenty on slicing and variables here on Stack Overflow (like when use negative number to slice a string in Python, 0 is disabled?) and online (like https://docs.python.org/3/library/functions.html?highlight=slice#slice) and I searched through ebooks like Introducing Python and Learning Python.
A plea:
Please do not tell me that what I want is wrong. I can slice mylist[0:6:1]
although there is no item indexed 6
in my example list, same for -7
. This is why I have this hunch that there might be numbers for x
and for y
too, which I could use in my variables.
You are welcome to tell me that it does not exist or cannot be done the way I would prefer. In the latter case, I am also asking you for alternatives, as close to my request as possible.
Background for those who want more:
This question is more about "figuring out slicing" than about making the printout possible. I do have workarounds, but would love to find out more.