DEV Community

loading...
Cover image for Day 12: List Shuffle

Day 12: List Shuffle

mattryanmtl profile image Matt Ryan ・1 min read

Shuffle a list.

import random

def shuffle_words(input_list):
    output_list = []
    while input_list:
        output_list.append(input_list.pop(random.randint(0, len(input_list)-1)))

    return output_list

if __name__ == '__main__':
    input_list = ["Alderaan", "Bespin", "Corellia", "Coruscant", "Dagobah",
                "Endor", "Geonosis", "Hoth", "Jakku", "Kashyyyk",
                "Mandalore", "Ryloth", "Scarif", "Tatooine", "Utapau", "Yavin"]
    print(shuffle_words(input_list))
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app