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>Your example that works:</p>

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

<p>uses Python <a href="https://docs.python.org/2.3/whatsnew/section-slices.html" rel="nofollow">slices notation</a> which is not a function in the sense that I assume you're requesting. Essentially <code>::</code> acts as a separator. A more verbose version of your code would be:</p>

<pre><code>y = x[len(x):None:-1]
</code></pre>

<p>or</p>

<pre><code>y = x[start:end:step]
</code></pre>

<p>I probably wouldn't be complaining that python makes your life really, really easily.</p>

<hr>

<p>Edit to be super pedantic. Someone could argue that calling <code>[]</code> at all is using an inbuilt python function because it's really syntactical sugar for the method <code>__getitem__()</code>.</p>

<pre><code>x.__getitem__(0) == x[0]
</code></pre>

<p>And using <code>::</code> does make use of the <code>slice()</code> object.</p>

<pre><code>x.__getitem__(slice(len(x), None, -1) == x[::-1]
</code></pre>

<p>But... if you were to argue this, anything you write in python would be using inbuilt python functions.</p>

python list python-3.x