Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot mutate a Python randomly generated list

I’m just playing around with a local k8s cluster and created a simple app to containerize.
I just want to print a reversed array.
Disclaimer: I might be using the words "lists" and "arrays" interchangeably here 🙂

So I defined two utility functions: one to generate lists with random int values and one to reverse said lists.

from random import randint

def reverse_array(input):
    i=0
    arr_length = len(input)
    last_index = arr_length - 1
    while i < arr_length / 2:
        temp = input[i]
        input[i] = input[last_index - i]
        input[last_index - i] = temp
        i=i+1
    return input

def generate_random_array(max_iteration):
    return [randint(0, 100) for _ in range(0, max_iteration)]

Both methods have unit tests, so I know they’re working as intended

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

then I execute them in the main app:

import util.misc as misc
from time import sleep

while True:
    input_arr = misc.generate_random_array(5)
    reversed_arr = misc.reverse_array(input_arr)
    
    # adding manual and control only for debugging
    manual = [1, 2, 3]
    control= misc.reverse_array(manual)
    print(f"your original array is {input_arr}")
    print(f"your reversed array is {reversed_arr}")
    print(f"control is {control}")
    sleep(1)

Oddly enough, the application’s output shows reversed_arr actually has the same order as input_arr, but control is correctly reversed.

your original array is [27, 95, 0, 91, 13]
your reversed array is [27, 95, 0, 91, 13]
control is [3, 2, 1]

What is going on here? It seems that arrays produced by generate_random_array are actually immutable

>Solution :

Your code actually reverses the array, you can see that by printing out the original array before reverse:

input_arr = generate_random_array(5)
print(f"your original array is {input_arr}")

reversed_arr = reverse_array(input_arr)
print(f"your reversed array is {reversed_arr}")

Output:

your original array is [15, 69, 31, 14, 34]
your reversed array is [34, 14, 31, 69, 15]

What happens is you’re mutating (reversing) the original array and also returning a link to the same object as "reversed" array, therefore you can’t see the difference when printing after the reverse.

You can also notice that if you change the reversed array, the original will change too. They’re just links to the same object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading