According to documentation:

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

Basically, it picks k unique random elements, a sample, from a sequence:

>>> import random
>>> c = list(range(0, 15))
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> random.sample(c, 5)
[9, 2, 3, 14, 11]

random.sample works also directly from a range:

>>> c = range(0, 15)
>>> c
range(0, 15)
>>> random.sample(c, 5)
[12, 3, 6, 14, 10]

In versions earlier than 3.11, random.sample works with sets too:

>>> c = {1, 2, 4}
>>> random.sample(c, 2)
[4, 1]

However, random.sample doesn't work with arbitrary iterators:

>>> c = [1, 3]
>>> random.sample(iter(c), 5)
TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).

In version 3.9, the counts parameter was added:

Repeated elements can be specified one at a time or with the optional keyword-only counts parameter. For example, sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5).

Answer from alecxe on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_random_sample.asp
Python Random sample() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ยท โฎ Random Methods ยท 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 ยป ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-random-sample-function
random.sample() function - Python - GeeksforGeeks
Explanation: random.sample(a, 3) selects 3 unique elements from list a. ... Returns: Returns a new list containing k randomly selected unique elements. Example 1: This code selects random characters from a string and returns them in a list.
Published ย  February 16, 2026
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ random.html
random โ€” Generate pseudo-random numbers
May 18, 2026 - Repeated elements can be specified one at a time or with the optional keyword-only counts parameter. For example, sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5).
๐ŸŒ
Computational and Inferential Thinking
inferentialthinking.com โ€บ chapters โ€บ 10 โ€บ 4 โ€บ random-sampling-in-python
Random Sampling in Python - Computational and Inferential Thinking
Just as sample did, so also np.random.choice gives you the entire sequence of sampled elements. You can use array operations to answer many questions about the sample. For example, you can find which actor was the second one to be drawn, or the number of faces of the die that appeared more than once.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ random โ€บ python random sample() to choose multiple items from any sequence
Python random.sample() to choose multiple items from any sequence
July 25, 2021 - The sample() function takes two ... if you miss any of the required arguments. In this example, we will choose three random items from a list....
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Random Sampling from a List in Python: random.choice, sample, choices | note.nkmk.me
May 19, 2025 - In Python, you can randomly sample elements from a list using the choice(), sample(), and choices() functions from the random module.
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-random-sample-method
Python Random sample() Method - Learn By Example
April 23, 2024 - Hereโ€™s how you can use the random.sample() method. The following example returns a list of five unique numbers randomly chosen from my_list.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python random.sample() with examples
Python random.sample() With Examples - Spark By {Examples}
May 31, 2024 - The random.sample() function in Python is a part of the random module which is used to generate a randomly selected sample of items from a given
Find elsewhere
Top answer
1 of 3
86

According to documentation:

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

Basically, it picks k unique random elements, a sample, from a sequence:

>>> import random
>>> c = list(range(0, 15))
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> random.sample(c, 5)
[9, 2, 3, 14, 11]

random.sample works also directly from a range:

>>> c = range(0, 15)
>>> c
range(0, 15)
>>> random.sample(c, 5)
[12, 3, 6, 14, 10]

In versions earlier than 3.11, random.sample works with sets too:

>>> c = {1, 2, 4}
>>> random.sample(c, 2)
[4, 1]

However, random.sample doesn't work with arbitrary iterators:

>>> c = [1, 3]
>>> random.sample(iter(c), 5)
TypeError: Population must be a sequence.  For dicts or sets, use sorted(d).

In version 3.9, the counts parameter was added:

Repeated elements can be specified one at a time or with the optional keyword-only counts parameter. For example, sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5).

2 of 3
3

random.sample() also works on text

example:

> text = open("textfile.txt").read() 

> random.sample(text, 5)

> ['f', 's', 'y', 'v', '\n']

\n is also seen as a character so that can also be returned

you could use random.sample() to return random words from a text file if you first use the split method

example:

> words = text.split()

> random.sample(words, 5)

> ['the', 'and', 'a', 'her', 'of']
๐ŸŒ
Interactive Chaos
interactivechaos.com โ€บ en โ€บ python โ€บ function โ€บ randomsample
random.sample | Interactive Chaos
March 11, 2021 - If this exists, it must contain the absolute frequency of each of the population elements, which means that the number of population elements and counts must coincide. So, for example, the following code: random.sample(["A", "B", "C"], k = 2, counts = [3, 2, 1])
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ random-sample-in-python
Python Random Sample from List - Complete Guide 2026
June 16, 2026 - Use random.sample(your_list, k) where k is the number of elements you want. This returns a new list of k unique elements chosen randomly without replacement. Example: random.sample([1, 2, 3, 4, 5], 3) might return [4, 1, 3].
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python random sample()
Python Random sample()- Scaler Topics
May 4, 2023 - Now, as the length parameter specified the length of the random sample output so the length parameter must be an integer else the Python interpreter raises the TypeError exception or error. Let us suppose that we have provided a string value in place of the integer then the error may look like this: Let us now take an example and see how the random sample() Python method works and returns a randomly selected sequence of a specified length.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ random-sampling-in-numpy-sample-function
Random sampling in numpy | sample() function - GeeksforGeeks
July 11, 2025 - # Python program explaining # numpy.random.sample() function # importing numpy import numpy as geek # output random value out_val = geek.random.sample() print("Output random value : ", out_val) ...
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ mastering random sampling in python
Mastering Random Sampling in Python | Towards Data Science
January 26, 2025 - The โ€˜random.sample()โ€™ method is useful for randomly sampling N items from a list. For example, if weโ€™d like to sample N=5 items from our BMI list we do the following:
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-randomsample-in-python
What is random.sample() in Python?
The random module in Python provides many options to generate random objects or numbers. The sample() function is used to create a random sample without replacement of a specified length, meaning that different objects will be selected from a sequence, but once an object has been selected, ...
๐ŸŒ
Kanaries
docs.kanaries.net โ€บ topics โ€บ Python โ€บ python-random-sample
Python Random Sampling: Tips and Techniques for Effective Data Analysis โ€“ Kanaries
August 17, 2023 - When dealing with large datasets, it's often impractical (and sometimes impossible) to analyze every single data point. This is where random sampling steps in. Python, a powerful and versatile language, provides the random.sample() function, a tool that allows you to create random samples from your data quickly and efficiently.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ random module โ€บ .sample()
Python | Random Module | .sample() | Codecademy
March 9, 2022 - The .sample() method returns a list of items that are randomly sampled from a given population. The returned list consists of randomly selected items without duplicates. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_random_sample_method.htm
Python random.sample() Method
This random.sample() method returns a list of k unique elements chosen randomly from the given sequence. Let's consider examples using any sequential data type such as a list, string, tuple, or set to randomly select unique elements from the sequence.