How can I convert this Python mutable function into immutable?
<p>I have a mutable Fibonacci function that mutates a list in order to return that list as a fib sequence. I want to do the same thing... but the list (or tuple in this case) should not be able to change. The return still has to send the whole list, how can I implement this, without using recursion. </p>

<p>Ex) if x = 6... list should return {1,1,2,3,5,8}</p>

<p>I'm running in python 3.5</p>

<p>Here is my code:</p>

<pre><code>def mutableFib(x):
result = []
for y in range(0,x):
    if y &lt; 2:
        result.append(1)
    else:
        last = result[y - 1]
        previous = result[y - 2]
        result.append(last + previous) //result is being changed
return result
</code></pre>

<p>If I understand what you mean by "a functional approach that doesn't use recursion," it seems like the input parameter should be a list of the first n Fibonacci numbers and the output should be a list of the first n+1 Fibonacci numbers.  If this is correct, then you could use</p>

<pre><code>def fibonacci(fib):
    if not fib: return [1]
    if len(fib) == 1: return [1,1]
    return fib + [fib[-1]+fib[-2]]
</code></pre>

python list immutability