random.shuffle() function in Python
Last Updated :
16 Aug, 2022
The shuffle() is an inbuilt method of the random module. It is used to shuffle a sequence (list). Shuffling a list of objects means changing the position of the elements of the sequence using Python.
Syntax of random.shuffle()
The order of the items in a sequence, such as a list, is rearranged using the shuffle() method. This function modifies the initial list rather than returning a new one.
Syntax: random.shuffle(sequence, function)
Parameters:
- sequence : can be a list
- function : optional and by default is random(). It should return a value between 0 and 1.
Returns: nothing
Python random.shuffle() function to shuffle list
Example 1:
Python3
# import the random module
import random
# declare a list
sample_list = ['A', 'B', 'C', 'D', 'E']
print("Original list : ")
print(sample_list)
# first shuffle
random.shuffle(sample_list)
print("\nAfter the first shuffle : ")
print(sample_list)
# second shuffle
random.shuffle(sample_list)
print("\nAfter the second shuffle : ")
print(sample_list)
Output :
Original list :
['A', 'B', 'C', 'D', 'E']
After the first shuffle :
['A', 'B', 'E', 'C', 'D']
After the second shuffle :
['C', 'E', 'B', 'D', 'A']
The shuffle() method cannot be used to shuffle immutable DataTypes like strings.
Example 2:
Python3
# import the random module
import random
# user defined function to shuffle
def sample_function():
return 0.5
sample_list = ['A', 'B', 'C', 'D', 'E']
print("Original list : ")
print(sample_list)
# as sample_function returns the same value
# each time, the order of shuffle will be the
# same each time
random.shuffle(sample_list, sample_function)
print("\nAfter the first shuffle : ")
print(sample_list)
sample_list = ['A', 'B', 'C', 'D', 'E']
random.shuffle(sample_list, sample_function)
print("\nAfter the second shuffle : ")
print(sample_list)
Output :
Original list :
['A', 'B', 'C', 'D', 'E']
After the first shuffle :
['A', 'D', 'B', 'E', 'C']
After the second shuffle :
['A', 'D', 'B', 'E', 'C']
Similar Reads
random.sample() function - Python sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5
2 min read
numpy.random.shuffle() in python With the help of numpy.random.shuffle() method, we can get the random positioning of different integer values in the numpy array or we can say that all the values in an array will be shuffled randomly. Syntax : numpy.random.shuffle(x) Return : Return the reshuffled numpy array. Example #1 : In this
1 min read
randint() Function in Python randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.Python randint() Method SyntaxSyntax: randint(sta
6 min read
Python Random - random() Function There are certain situations that involve games or simulations which work on a non-deterministic approach. In these types of situations, random numbers are extensively used in the following applications: Creating pseudo-random numbers on Lottery scratch cardsreCAPTCHA on login forms uses a random nu
3 min read
random.expovariate() function in Python random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.expovariate() expovariate() is an inbuilt method of the random module. It is used to re
2 min read