30 seconds of python

  Back to Home

shuffle

listrandomintermediate

Randomizes the order of the values of an list, returning a new list.

Uses the Fisher-Yates algorithm to reorder the elements of the list.

from copy import deepcopy
from random import randint

def shuffle(lst):
  temp_lst = deepcopy(lst)
  m = len(temp_lst)
  while (m):
    m -= 1
    i = randint(0, m)
    temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
  return temp_lst