Menu

gitpiper

shuffle python Code Snippet in 2022

listrandomadvanced

Last Updated: 25 September 2022

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.
  • random.shuffle provides similar functionality to this snippet.
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
foo = [1, 2, 3] shuffle(foo) # [2, 3, 1], foo = [1, 2, 3]

python snippet similar to shuffle For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️