ValueError: Sample larger than population or is negative

avatar
Borislav Hadzhiev

Last updated: Feb 22, 2023
3 min

banner

# ValueError: Sample larger than population or is negative

The "ValueError: Sample larger than population or is negative" occurs when we use the random.sample() method to select more unique random elements than there are values in the list.

To solve the error, use the random.choices() method instead.

valueerror sample larger than population or is negative

Here is an example of how the error occurs.

main.py
import random a_list = ['bobby', 'hadz', 'com'] # โ›”๏ธ ValueError: Sample larger than population or is negative random_elements = random.sample(a_list, 4)

using random sample incorrectly

The list only contains 3 elements, but we tried to select 4 random elements.

The random.sample() method returns a list of N unique elements chosen from the provided sequence.

The first argument the method takes is a sequence and the second is the number of random elements to be returned.

# Using the min() function to solve the error

One way to solve the error is to use the min() function.

main.py
import random a_list = ['bobby', 'hadz', 'com'] random_elements = random.sample(a_list, min(4, len(a_list))) print(random_elements) # ๐Ÿ‘‰๏ธ ['com', 'bobby', 'hadz']

using min function to solve the error

The code for this article is available on GitHub

The min function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
result = min(10, 5, 20) print(result) # ๐Ÿ‘‰๏ธ 5

We used the function to return the list's length if N exceeds the list's length.

When using this approach at most len(list) random element can be selected.

# Using random.choices() method to solve the error

You can also use the random.choices() method to solve the error.

main.py
import random a_list = ['bobby', 'hadz', 'com'] random_elements = random.choices(a_list, k=4) print(random_elements) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'bobby', 'hadz']

using random choices method to solve the error

The code for this article is available on GitHub

The random.choices() method returns a k sized list of elements chosen from the provided iterable with replacement.

With replacement basically means that the same element can be returned multiple times.

If the iterable is empty, the method raises an IndexError exception.

On the other hand, the random.sample() method is used for random sampling without replacement.

# Getting a single random element from a sequence

If you need to get a single random element from a sequence, use the random.choice() method.

main.py
import random a_list = ['bobby', 'hadz', 'com'] random_element = random.choice(a_list) print(random_element) # ๐Ÿ‘‰๏ธ hadz

getting single random element from sequence

The code for this article is available on GitHub

The random.choice() method returns a random element from a non-empty sequence.

If the provided sequence is empty, an IndexError exception is raised.

# Using a try/except statement to handle the IndexError

If you need to handle a potential IndexError exception, use a try/except statement.

main.py
import random a_list = [] try: random_element = random.choice(a_list) print(random_element) # ๐Ÿ‘‰๏ธ hadz except IndexError: print('The sequence is empty')

using try except statement to handle the error

The code for this article is available on GitHub

The list in the example is empty so an IndexError is raised and is then handled by the except block.

Want to learn more about getting or generating random values in Python? Check out these resources: Get random Key and Value from a Dictionary in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev