0

I have this script that should make two different lists. but instead, it changes both of them so they're the same.

from random import shuffle

my_list = [1,2,3,4]
second_list = []
shuffle(my_list)
second_list.extend(my_list)

print(my_list,second_list)

How do I prevent this from happening?

1 Answers1

0

Assuming you want the original and the shuffled list at the output, you have to shuffle after saving the original list

from random import shuffle

my_list = [1,2,3,4]
second_list = []
second_list.extend(my_list)
shuffle(my_list)

print(my_list,second_list)

Or if you want 2 shuffled lists:

from random import shuffle

my_list = [1,2,3,4]
second_list = []
second_list.extend(my_list)
shuffle(my_list)
shuffle(second_list)

print(my_list,second_list)
the-veloper
  • 304
  • 1
  • 7
  • yeah, that's because you were copying the list after shuffling it. – the-veloper Nov 02 '20 at 12:13
  • In another project im having a really large list. would there be a easier way to do this without having to shuffle every part of it? – RichStar03 Nov 02 '20 at 12:14
  • That's on another topic, but check this answer out: https://stackoverflow.com/a/44862688/10116997 It initializes a RNG for a minute, but calls to it are extremely fast. – the-veloper Nov 02 '20 at 12:15