DEV Community

Cover image for Day 12: List Shuffle
Matt Ryan
Matt Ryan

Posted on

Day 12: List Shuffle

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

Top comments (0)