Python Number shuffle() Method


Contents

Description

The shuffle() method in Python number randomizes the items within a list in place. Python Random shuffle() Method in Python programming takes a sequence i.e. list, string, or tuple and further reorganizes the items present there in order. This method of Python Random Module is supposed to change the original tuple/list/string, and it does not return the new tuple/list/string.

Syntax

Here is the syntax for shuffle() method:

shuffle (lst )

Note − One cannot access this function directly, so it is required to import the random module in Python and then we are supposed to call this function making use of the random static object.

Parameters

  • lst − This can be a list or tuple.

Return Value

This method in Python random number functions does not return any value.

Example

The example below shows the usage of shuffle() method.

#!/usr/bin/python
import random

list = [20, 16, 10, 5];
random.shuffle(list)
print "Reshuffled list : ",  list

random.shuffle(list)
print "Reshuffled list : ",  list

When we run above program, it produces following result:

Reshuffled list :  [16, 5, 10, 20]
Reshuffled list :  [16, 5, 20, 10]

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial