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>Here's a solution that doesn't use built-in <em>functions</em> but relies on list methods. It reverse in-place, as implied by your specification:</p>

<pre><code>&gt;&gt;&gt; x = [1,2,3,4]
&gt;&gt;&gt; def reverse(seq):
...   temp = []
...   while seq:
...     temp.append(seq.pop())
...   seq[:] = temp
... 
&gt;&gt;&gt; reverse(x)
&gt;&gt;&gt; x
[4, 3, 2, 1]
&gt;&gt;&gt; 
</code></pre>

<h2>ETA</h2>

<p>Jim, your answer using <code>insert</code> at position 0 was driving me nuts! That solution is quadratic time! You can use <code>append</code> and <code>pop</code> with a temporary list to achieve linear time using simple list methods. See (<code>reverse</code> is in blue, <code>rev</code> is green):
<a href="http://i.stack.imgur.com/IiqWK.png" rel="nofollow"><img src="http://i.stack.imgur.com/IiqWK.png" alt=""></a></p>

<p>If it feels a little bit like "cheating" using <code>seq[:] = temp</code>, we could always loop over <code>temp</code> and append every item into <code>seq</code> and the time complexity would still be linear but probably slower since it isn't using the C-based internals.</p>

python list python-3.x