There are quite a few questions on stack overflow regarding the random.shuffle
method of the random module.
Something that irks me about shuffle
is that it shuffles in-place rather than returning a shuffled copy.
Note that shuffle works in place, and returns None.
Therefore expressions like
for index, (parent1, parent2) in enumerate(zip(sorted(population)[::2], shuffle(population)[1::2])):
don't work. Writing it with a side effect seems unnecessarily verbose:
other_half = population[1::2]
random.shuffle(other_half)
for index, (parent1, parent2) in enumerate(zip(sorted(population)[::2], other_half):
What's a pythonic way of functionally shuffling a list?