Skip to main content

Command Palette

Search for a command to run...

Shuffle Function in Python

Published
1 min readView as Markdown
Shuffle Function in Python
A

I am a Tech enthusiast interested to learn new things from everyone and post my learning in public.

Randomizes the order of the values of a 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
#input and output
foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]

More from this blog

D

DEVS-OnThe-PUB

18 posts

The Content here will surely impact you, even if its in a small way. A place where Things are without bias...