vendredi 21 juin 2019

Function is unable to transform a list element if wrapped in a for function

Can someone explain the behavior of the following code?

import random


abc = ['a', 'b', 'c']
xyz = ['x', 'y', 'z']


def randomize(x):
    random.shuffle(x)
    x = x[0]
    print('expected: ' + str(x))
    return x


random_list = [abc, xyz]

for r in random_list:
    r = randomize(x=r)


print('-----\n')

print('actual: ' + str(abc))
print('actual: ' + str(xyz))

I would expect the return to match what is being printed in the function, however, that is not the case.




Aucun commentaire:

Enregistrer un commentaire