Python Random choice() and choices() Methods Tutorial

Python provides the random module in order to provide different random functions and methods. random.choice() method is one of the most popular method which is used to select random item from the given list. In this tutorial we will learn different use cases about the random.choice() method.

random.choice() Method Syntax

random.choice() method is provided via the random module as expected. It has the following syntax.

choice(sequence)
  • sequence is an iterable variable which contains single or multiple items.

random.choices() Method Syntax

Even it may seem a similar choice() and choices() methods are similar but the choices() method provides more advanced usage. Below you can see the choices() method syntax in detail.

choices( iterable , weights , * , cum_weights, k)
  • iterable is a list of items where single or multiple of them will be selected randomly.
  • weights is the weight of the given iterable list.
  • cum_weights is a sequence to provides weights.
  • k is the number of the elements to be selected which is by default 1.

Select Random Item From List

We will start with a simple example where we will select a single random item from the given list. This list can be an integer list, string list or mixed list. We will use both the choice() and choices() methods.

import random

number_list = [ 1 , 2 , 3 , 4 , 5 ]
name_list = [ "ali" , "ahmet" , "elif" ]
mixed_list = [ "ali" , "ahmet" , "elif" , 1 , 2 , 3 ]

print(random.choice(number_list))
print(random.choice(name_list))
print(random.choice(mixed_list))

print(random.choices(number_list))
print(random.choices(name_list))
print(random.choices(mixed_list))
Select Random Item From List

Select Multiple Items From List Randomly

We can also select multiple items from given list randomly. choices() method provides this ability where we can provide number of items we want to randomly select. We will use the k parameter of the choice() method.

import random

number_list = [ 1 , 2 , 3 , 4 , 5 ]
name_list = [ "ali" , "ahmet" , "elif" ]
mixed_list = [ "ali" , "ahmet" , "elif" , 1 , 2 , 3 ]

print( random.choices( number_list , k=2 ) )
print( random.choices( name_list , k=2 ) )
print( random.choices( mixed_list , k=2 ) )
Select Multiple Items From List Randomly

Select Random Item From Set

The tuple is another popular type used in Python. We can use the random.choise() and random.choices() method to select a random item from the given tuple.

import random

number_tuple = ( 1 , 2 , 3 , 4 , 5 )
name_tuple = ( "ali" , "ahmet" , "elif" )
mixed_tuple = ( "ali" , "ahmet" , "elif" , 1 , 2 , 3 )

print(random.choice(number_tuple))
print(random.choice(name_tuple))
print(random.choice(mixed_tuple))

print(random.choices(number_tuple))
print(random.choices(name_tuple))
print(random.choices(mixed_tuple))
Select Random Item From Set

Select Random Item From Set

The set is another popular type used in Python. We can use the random.choise() and random.choices() method to select a random item from the given set.

import random

number_set = ( 1 , 2 , 3 , 4 , 5 )
name_set = ( "ali" , "ahmet" , "elif" )
mixed_set = ( "ali" , "ahmet" , "elif" , 1 , 2 , 3 )

print(random.choice(number_set))
print(random.choice(name_set))
print(random.choice(mixed_set))

print(random.choices(number_set))
print(random.choices(name_set))
print(random.choices(mixed_set))
Select Random Item From Set

Select Random Integer From Specified Range

Even the random module provides different methods in order to select random numbers for a specific range we can also use the choice() and choices() methods in order to select single or multiple numbers with the help of range() method.

import random

print( random.choice( range( 0 , 10 ) ) )

print( random.choices( range( 0 , 10 ) ) )

print( random.choices( range( 0 , 10 ) , k=3 ) )
Select Random Integer From Specified Range

Select Random Boolean (True or False)

random.choice() and random.choices() methods can be used to select single or multiple random boolean values. Even there are 2 different boolean values True and False they are very popular for different use cases.

import random

print( random.choice( [ True , False ] ) )

print( random.choices( [ True , False ] ) )

print( random.choices( [ True , False ] , k=3 ) )
Select Random Boolean (True or False)

1 thought on “Python Random choice() and choices() Methods Tutorial”

  1. please the random choice method does not support set in python. it only accepts sequencial list like list tuple array bytes and bytes array in python. so please check. the reason is that the set is not subscriptable. thank eritten from ghana.

    Reply

Leave a Comment