❮ Random Methods
Example
Shuffle a list [reorganize the order of the list items]:
import random
mylist = ["apple", "banana", "cherry"]
random.shuffle[mylist]
print[mylist]
Try it Yourself »
Definition and Usage
The shuffle[]
method takes a sequence, like a list, and reorganize the order of the items.
Note: This method changes the original list, it does not return a new list.
Syntax
random.shuffle[sequence, function]
Parameter Values
sequence | Required. A sequence. |
function | Optional. The name of a function that returns a number between 0.0 and 1.0. If not specified, the function
random[] will be used
|
More Examples
Example
You can define your own function to weigh or specify the result.
If the function returns the same number each time, the result will be in the same order each time:
import random
def myfunction[]:
return 0.1
mylist = ["apple", "banana", "cherry"]
random.shuffle[mylist, myfunction]
print[mylist]
Try it Yourself »
❮ Random Methods
In Python, you can shuffle [= randomize] a list, string, and tuple with random.shuffle[]
and random.sample[]
.
- random — Generate pseudo-random numbers — Python 3.8.1 documentation
random.shuffle[]
shuffles a list in place, and random.sample[]
returns a new randomized list. random.sample[]
can also be used for a string and tuple.
random.shuffle[]
shuffles a list in placerandom.sample[]
returns a new shuffled list- How to shuffle a string and tuple
- Initialize the random number generator with
random.seed[]
If you want to sort in ascending or descending order or reverse instead of shuffling, see the following articles.
- Sort a list, string, tuple in Python [sort, sorted]
- Reverse a list, string, tuple in Python [reverse, reversed]
random.shuffle[]
shuffles a list in place
You can shuffle a list in place with random.shuffle[]
.
import random
l = list[range[5]]
print[l]
# [0, 1, 2, 3, 4]
random.shuffle[l]
print[l]
# [1, 0, 4, 3, 2]
random.sample[]
returns a new shuffled list
random.sample[]
returns a new shuffled list. The original list remains unchanged.
random.sample[]
returns random elements from a list. Pass the list to the first argument and the number of elements to return to the second
argument. See the following article for details.
- Random sampling from a list in Python [random.choice, sample, choices]
By setting the total number of elements in the list to the second argument, random.sample[]
returns a new list with all elements randomly shuffled. You can get the total number of elements in the list with len[]
.
l = list[range[5]]
print[l]
# [0, 1, 2, 3, 4]
lr = random.sample[l, len[l]]
print[lr]
# [0, 3, 1, 4, 2]
print[l]
# [0, 1, 2, 3, 4]
How to shuffle a string and tuple
Strings and tuples are immutable, so random.shuffle[]
that modifies the original object raises an error TypeError
.
s = 'abcde'
# random.shuffle[s]
# TypeError: 'str' object does not support item assignment
t = tuple[range[5]]
print[t]
# [0, 1, 2, 3, 4]
# random.shuffle[t]
# TypeError: 'tuple' object does not support item assignment
To shuffle strings or tuples, use random.sample[]
, which creates a new object.
random.sample[]
returns a list even when a string or tuple is specified to the first argument, so it is necessary to convert it to a string or tuple.
For strings, a list of characters is returned. Use the join[]
method to concatenate to a single string again.
- Concatenate strings in Python [+ operator, join, etc.]
sr = ''.join[random.sample[s, len[s]]]
print[sr]
# bedca
Use tuple[]
for tuples, which creates a tuple from a list.
- Convert list and tuple to each other in Python
tr = tuple[random.sample[t, len[l]]]
print[tr]
# [0, 1, 2, 4, 3]
Initialize the random number generator with random.seed[]
You can initialize a random number generator with random.seed[]
.
After initializing with the same seed, it is shuffled in the same way.
l = list[range[5]]
random.seed[0]
random.shuffle[l]
print[l]
# [2, 1, 0, 4, 3]
l = list[range[5]]
random.seed[0]
random.shuffle[l]
print[l]
# [2, 1, 0, 4, 3]