If all you want is to get back to the original input of [1,2,3,4]
, it's easy. Just don't clobber the original with your sample. Put the results of sampling into a different variable:
import random
my_data = [1,2,3,4]
print(f"original before sampling: {my_data}")
sampled_data = random.sample(my_data, 4)
print(f"after sampling, original: {my_data}, sample: {sampled_data}")
produces, for example:
original before sampling: [1, 2, 3, 4]
after sampling, original: [1, 2, 3, 4], sample: [2, 1, 3, 4]
If you want a do-over to reproduce the same sample, then you should use gestate()/setstate() to reset the pseudo-random number generator's internal state in addition to preserving the original input:
import random
my_data = [1,2,3,4]
print(f"original before sampling: {my_data}")
rng_state = random.getstate()
sampled_data = random.sample(my_data, 4)
print(f"after sampling, original: {my_data}, sample: {sampled_data}")
sampled_data = random.sample(my_data, 4)
print(f"without resetting PRNG, original: {my_data}, sample: {sampled_data}")
random.setstate(rng_state)
sampled_data = random.sample(my_data, 4)
print(f"after resetting PRNG, original: {my_data}, sample: {sampled_data}")
produces, for example:
original before sampling: [1, 2, 3, 4]
after sampling, original: [1, 2, 3, 4], sample: [2, 1, 3, 4]
without resetting PRNG, original: [1, 2, 3, 4], sample: [3, 4, 1, 2]
after resetting PRNG, original: [1, 2, 3, 4], sample: [2, 1, 3, 4]