Reverse a list without using built-in functions
<p>I'm using Python 3.5. </p>

<p>As part of a problem, I'm trying to design a function that takes a list as input and reverts it. So if <code>x = [a, b, c]</code> the function would make <code>x = [c, b, a]</code>.</p>

<p>The problem is, I'm not allowed to use any built-in functions, and it has got me stuck. My initial thought was the following loop inside a function:</p>

<pre><code>for revert in range(1, len(x) + 1):
    y.append(x[-revert])
</code></pre>

<p>And it works. But the problem is I'm using <code>len(x)</code>, which I believe is a built-in function, correct? </p>

<p>So I searched around and have made the following very simple code:</p>

<pre><code>y = x[::-1]
</code></pre>

<p>Which does exactly what I wanted, but it just seems almost too simple/easy and I'm not sure whether <code>"::"</code> counts as a function. </p>

<p>So I was wondering if anyone had any hints/ideas how to manually design said function? It just seems really hard when you can't use any built-in functions and it has me stuck for quite some time now.</p>

<p><code>::</code> is not a function, it's a <em>python literal</em>. as well as <code>[]</code></p>

<p>How to check if <code>::, []</code> are functions or not. Simple,</p>

<pre><code>    import dis
    a = [1,2]
    dis.dis(compile('a[::-1]', '', 'eval'))
      1           0 LOAD_NAME                0 (a)
                  3 LOAD_CONST               0 (None)
                  6 LOAD_CONST               0 (None)
                  9 LOAD_CONST               2 (-1)
                 12 BUILD_SLICE              3
                 15 BINARY_SUBSCR
                 16 RETURN_VALUE
</code></pre>

<p>If <code>::,[]</code> were functions, you should find a label <code>CALL_FUNCTION</code> among python instructions executed by <code>a[::-1]</code> statement. So, they aren't. </p>

<p>Look how python instructions looks like when you call a function, lets say <code>list()</code> function</p>

<pre><code>&gt;&gt;&gt; dis.dis(compile('list()', '', 'eval'))
  1           0 LOAD_NAME                0 (list)
              3 CALL_FUNCTION            0
              6 RETURN_VALUE
</code></pre>

<p>So, basically</p>

<pre><code>def rev(f):
    return f[::-1]
</code></pre>

<p>works fine. But, I think you should do something like Jim suggested in his answer if your question is a homework or sent by you teacher. But, you can add this quickest way as a side note.</p>

<p>If you teacher complains about <code>[::-1]</code> notation, show him the example I gave you. </p>

python list python-3.x