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>You could create a new list for every new element.</p>

<pre><code>def fibonacci(x):
    list_of_fibonaccis = []
    for y in range(0,x):
        if y &lt; 2:
            temp_fibonacci = [1] * (y + 1)
        else:
            last = list_of_fibonaccis[y-1][-1]
            previous = list_of_fibonaccis[y-1][-2]
            temp_fibonacci = list_of_fibonaccis[y-1] + [last + previous]
        list_of_fibonaccis.append(temp_fibonacci)
    return list_of_fibonaccis[-1]
</code></pre>

python list immutability