Python random choice














































Python random choice



Python random choice() 


In this article, I will let you know how to 
select a random item from a list and other sequence types in Python. 
choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string.
Python random module's  random.choice() function returns a random element from the non-empty sequence. we can use the random.choice() function for selecting a random password from word-list, Selecting a random item from the available data.

Syntax :

random.choice(sequence)


Parameter Values

ParameterDescription
sequenceRequired. A sequence like a list, a tuple, a range of numbers etc.


Return Value : -This function returns a single item from the sequence. If we pass an empty list or sequence to random.choice() function it will raise IndexError: Cannot choose from an empty sequence.


 Examples :


1.
#importing random module
import random

x = "CPPSECRETS"

print("Random element from sequence x : ",random.choice(x))
Output :
Random element from sequence x :  T

2.
#importing random module 
import random 

list = [12345678910

for x in range(4): 
    print(random.choice(list))
Output :
5                                                                                                                                             
5                                                                                                                                             
7                                                                                                                                             
9


3. The random choice from a set in Python

If we pass Set object directly to the choice function we will get the TypeError: set's object does not support indexing.Using the random.choice function we can't choose random items directly from a set, without having to copy it into a tuple. Let see how to use a choice function to select a random item from Set.

 #importing random module 
import random

mass_set = {8330551362}

#set converting into tuple
mass = random.choice(tuple(mass_set))
print ("Randomly item from Set is  ", mass)

#directly print the random number from set 
print ( "Randomly item from Set directly : ",random.choice(mass_set))

Output




4. The random choice from Python Dictionary

Let see how to use the random.choice() function to select random key-value pair from a Python dictionary. The random.choice() function of a random module doesn't accept a dictionary, you need to convert a dictionary into a list before passing it to random.choice() function. Let see the example of how to choose dict keys randomly.

#importing random module
import random

#initializing a dictionary
dictionary = {
  "Kelly"50,
  "Red"68,
  "John"70,
  "Emma" :40
}

#assigning a random to key after converting dictionary into list
key = random.choice(list(dictionary))

print ("Random key-value pair from dictonary is ", key,":",dictionary[key])

Print ("Directly printing a random number from dictionary :",random.choice(dictionary))

Output




**********END OF ARTICLE*********



More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 114 0
Python string swapcase 92 0
Python string title 97 0
Python string startswith 102 0
Python string replace 144 0
Python string translate 85 0
Python string rpartition 85 0
Python string partition 74 0
Python string splitlines 114 0
Python string rsplit 79 0
Python string split 75 0
Python string rindex 81 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 81 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 91 0
Python string lstrip 79 0
Python string rjust 96 0
Python string ljust 95 0
Python string len 82 0
Python string join 75 0
Python string casefold 82 0
Python string isprintable 84 0
Python string encode 78 0
Python string isdecimal 84 0
Python string isidentifier 78 0
Python string isupper 77 0
Python string istitle 80 0
Python string isspace 100 0
Python string isnumeric 101 0
Python string isdigit 82 0
Python string islower 84 0
Python string isalpha 96 0
Python string isalnum 102 0
Python string index 87 0
Python string find 93 0
Python string expandtabs() 81 0
Python string endswith 91 0
Python string count 86 0
Python string capitalize 77 0
Python string center 84 0
Python string Introduction 98 0
Python string Template 140 0
Python string Formatter 144 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 104 0
Python string octdigits 86 0
Python string hexdigits 104 0
Python string digits 79 0
Python string ascii_letters 344 0
Python string ascii_lowercase 147 0
Python string ascii_uppercase 156 0
How to Create Download Manager in Python 523 0
Python random weibullvariate 164 0
python random paretovariate 133 0
Python random vonmisesvariate 178 0
Python random normalvariate 196 0
Python random lognormvariate 120 0
Python random gauss 120 0
Python random gammavariate 131 0
Python random expovariate 194 0
Python random betavariate 184 0
Python random triangular 116 0
Python random uniform 141 0
Python random random 117 0
Python random sample 182 0
Python random shuffle 178 0
Python random choices 234 0
Python random choice 166 0
Python random randint 148 0
Python random randrange 214 0
Python random getrandbits 140 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0

Comments