title: How to use variables for slicing Python lists?
body: <p>I have got a harmless list "mylist" of six items. Never empty, never any beastly items.</p>
  
  <pre><code>mylist = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
  </code></pre>
  
  <p>I found that there are two ways to index a list for slicing purposes. For this question I shall call them <strong>upper index (from 0 to 5)</strong> and <strong>lower index (from -1 to -6)</strong>. Here is a quick diagram (I had posted a photo with colors, which was replaced by another user):</p>
  
  <pre><code> &lt;&lt;&lt;&lt;|_________mylist[5:?:-1]_____________|
  
       |___mylist[0:3:1]______|&gt;&gt;&gt;&gt;&gt;&gt;
   ?
  (x)   0      1      2      3      4      5     (6)
  
       aaa    bbb    ccc    ddd    eee    fff
  
  (-7) -6     -5     -4     -3     -2     -1     (y)
                                                  ?
               &lt;&lt;&lt;&lt;&lt;&lt;|___mylist[-1:-5:-1]___|
  
       |______mylist[-6:?:1]________________|&gt;&gt;&gt;&gt;&gt;
  </code></pre>
  
  <p>For easily slicing at the start of my list, I can use the upper index like this:</p>
  
  <pre><code>&gt;&gt;&gt; mylist[0:3:1]
  ['aaa', 'bbb', 'ccc']
  </code></pre>
  
  <p>For easily slicing near the end, I find the lower index helpful, like:</p>
  
  <pre><code>&gt;&gt;&gt; mylist[-1:-5:-1]
  ['fff', 'eee', 'ddd', 'ccc']
  </code></pre>
  
  <p>By "easily" I mean for example the aspect of not concerning my code with the length of the list.</p>
  
  <p>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).</p>
  
  <p>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 <strong>backwards</strong> (in the console), so that when the script ends, the best <code>n</code> results will still be visible on the screen. So my step is <code>-1</code> in this context.</p>
  
  <p>I would like to use variables, to allow for different needs for different users (and different screen sizes). <strong>For debugging and for certain users I want to be able to print all results, i.e. the entire list backwards.</strong> I would like something in this syntax:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p><strong>My question is please, how do I write the first(topmost) slice in the diagram, <em>by using variables</em> and <em>by using the upper list</em> of indices?</strong> Or in other words, <strong>what is the numerical value of <code>x</code> (and <code>y</code>)</strong> in the diagram?</p>
  
  <p>Semi-solutions that I do not want to use:</p>
  
  <pre><code>mylist[5::-1]       # no variable at all
  
  mylist[5:None:-1]   # does not fit in my normal function for printing
  </code></pre>
  
  <p>I have not managed to use <code>None</code> in any simple operations with my variables, it gives me errors like:</p>
  
  <pre><code>    end = None - 20 
  TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
  </code></pre>
  
  <p>And I have not managed to convert <code>None</code> to any integer or other number. Is there a way? Would probably be a sacrilege in Pythonland...</p>
  
  <p><strong>If I cannot find the secret number of <code>x</code>, then I might need to work with this:</strong></p>
  
  <pre><code>mylist[-1:-7:-1]  # I want to use the upper index rather
  </code></pre>
  
  <p>But this approach involves checking the length of <code>mystring</code>.</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>What I have done before asking you:</strong><br>
  I have written a testing-script and have tried guessing <code>x</code>. I have searched and read up plenty on slicing and variables here on Stack Overflow (like  <a href="https://stackoverflow.com/q/31740252/5227835">when use negative number to slice a string in Python, 0 is disabled?</a>) and online (like <a href="https://docs.python.org/3/library/functions.html?highlight=slice#slice" rel="nofollow noreferrer">https://docs.python.org/3/library/functions.html?highlight=slice#slice</a>) and I searched through ebooks like Introducing Python and Learning Python.</p>
  
  <p><strong>A plea:</strong><br>
  Please do not tell me that what I want is wrong. I can slice <code>mylist[0:6:1]</code> although there is no item indexed <code>6</code> in my example list, same for <code>-7</code>. This is why I have this hunch that there might be numbers for <code>x</code> and for <code>y</code> too, which I could use in my variables.  </p>
  
  <p>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.</p>
  
  <p><strong>Background for those who want more:</strong><br>
  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.</p>