Randomly Shuffle Words List Function

  • Share this:

Code introduction


This function takes a list of words as an argument and then uses the shuffle method from the random module to randomly shuffle the order of the words in the list.


Technology Stack : os, sys, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import sys
import random

def shuffle_words(words):
    """
    Randomly shuffle a list of words.
    """
    shuffled_words = words[:]
    random.shuffle(shuffled_words)
    return shuffled_words

# Example usage
words = ['apple', 'banana', 'cherry', 'date', 'elderberry']
shuffled_words = shuffle_words(words)
print(shuffled_words)                
              
Tags: