Python Random sample() Method
Example
Return a list that contains any 2 of the items from a list:
import random
mylist = ["apple", "banana", "cherry"]
print(random.sample(mylist, k=2))
Try it Yourself »
Definition and Usage
The sample()
method returns a list with a
specified number of randomly selected items from a sequence.
Note: This method does not change the original sequence.
Note: The specified number (k=2
) cannot be longer than the length of the original sequence.
Syntax
random.sample(sequence, k)
Parameter Values
Parameter | Description |
---|---|
sequence | Required. A sequence. Can be any sequence: list, set, range etc. |
k | Required. The size of the returned list |