Open In App
Related Articles

random.choices() method in Python

Improve Article
Improve
Save Article
Save
Like Article
Like

The choices() method returns multiple random elements from the list with replacement. You can weigh the possibility of each result with the weights parameter or the cum_weights parameter. The elements can be a string, a range, a list, a tuple or any other kind of sequence.

Syntax : random.choices(sequence, weights=None, cum_weights=None, k=1)

Parameters :
1. sequence is a mandatory parameter that can be a list, tuple, or string.
2. weights is an optional parameter which is used to weigh the possibility for each value.
3. cum_weights is an optional parameter which is used to weigh the possibility for each value but in this the possibility is accumulated
4. k is an optional parameter that is used to define the length of the returned list.

Note: This method is different from random.choice().

Example:




import random
  
mylist = ["geeks", "for", "python"]
  
print(random.choices(mylist, weights = [10, 1, 1], k = 5))


Note: Every time output will be different as the system returns random elements.
Output:

['geeks', 'geeks', 'geeks', 'for', 'for']

Practical application: Print a random list with 6 items.




import random
  
mylist = ["apple", "banana", "mango"]
  
print(random.choices(mylist, weights = [10, 1, 1], k = 6))


Note: The output changes every time as choices() function is used.
Output:

['apple', 'banana', 'apple', 'apple', 'apple', 'banana']
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 27 Jan, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials