Python random sample














































Python random sample



Python random sample() 

 

In this article, we will learn how to use the random.sample() function to choose multiple items from a list, set, and dictionary. Python's random module provides random.sample() function for random sampling and randomly pick more than one element from the list without repeating elements. The random.sample() returns a list of unique elements chosen randomly from the list, sequence, or set, we call it random sampling without replacement.


Syntax :

random.sample(sequencek)

Parameter Values

ParameterDescription
sequenceRequired. A sequence. Can be any sequence: list, set, range etc.
kRequired. The size of the returned list
 

Return Value :

k length new list of elements chosen from the sequence.

- A random.sample() function raises a type error if you miss any of the required arguments.


EXAMPLES :

1. Simple implementation of sample() function.
# Python3 program to demonstrate 
# the use of sample() function . 

# import random 
from random import sample 

# Prints list of random items of given length 
list1 = [12345

print(sample(list1,3)) 

Output : 

[5, 4, 2]





2. Basic use of sample() function.

# Python3 program to demonstrate 
# the use of sample() function . 

# import random 
import random 


# Prints list of random items of 
# length 3 from the given list. 
list1 = [123456
print("With list:", random.sample(list1, 3)) 

# Prints list of random items of 
# length 4 from the given string. 
string = "Cppsecrets"
print("With string:", random.sample(string, 4)) 

# Prints list of random items of 
# length 4 from the given tuple. 
tuple1 = ("vishal""cpp""computer""science"
                "portal""secrets""btech"
print("With tuple:", random.sample(tuple1, 4)) 


# Prints list of random items of 
# length 3 from the given set. 
set1 = {"c""p""p""s""e""c""r""e""t""s"
print("With set:", random.sample(set1, 3)) 

Output: 

With list: [8106]                                                                                                                          
With string: ['c''r''t''t']                                                                                                             
With tuple: ['portal''vishal''science''secrets']                                                                                           
With set: ['r''t''c']





Note: As you can see the random.sample() function doesn't repeat the items in the result list. This is also called a random sample without replacement. If you want to generate random samples without replacement out of a list or population then you should use random.sample().

Points to remember about Python random.sample()

  • It is used for random sampling without replacement.
  • It doesn't change the specified sequence or list. It returns a new sampled list containing elements from the specified sequence or list.
  • The specified list or population need not be hashable or unique.

Important Note:

If your list itself contains repeated or duplicate elements, then random.sample() can pick repeated items because each occurrence is a possible selection in the sample. I.e., The random.sample() can pick the repeated items from the specified list if the unique members are less than a sampling size.


random.sample() function Error and exception

A sample function can raise the following two errors.

  • ValueError If the sample size is larger than the population (i.e., list or set) size
  • TypeError if any of the two arguments is missing.
Here is the example


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 103 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 76 0
Python string rindex 82 0
Python string rfind 106 0
Python string upper 80 0
Python string lower 82 0
Python string maketrans 90 0
Python string strip 81 0
Python string rstrip 92 0
Python string lstrip 80 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 85 0
Python string encode 78 0
Python string isdecimal 85 0
Python string isidentifier 78 0
Python string isupper 78 0
Python string istitle 80 0
Python string isspace 100 0
Python string isnumeric 102 0
Python string isdigit 83 0
Python string islower 84 0
Python string isalpha 96 0
Python string isalnum 102 0
Python string index 88 0
Python string find 93 0
Python string expandtabs() 81 0
Python string endswith 92 0
Python string count 86 0
Python string capitalize 78 0
Python string center 85 0
Python string Introduction 99 0
Python string Template 140 0
Python string Formatter 145 0
Python string printable 199 0
Python string whitespace 146 0
Python string punctuation 104 0
Python string octdigits 87 0
Python string hexdigits 104 0
Python string digits 80 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 524 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 186 0
Python random triangular 117 0
Python random uniform 141 0
Python random random 117 0
Python random sample 183 0
Python random shuffle 179 0
Python random choices 234 0
Python random choice 166 0
Python random randint 148 0
Python random randrange 214 0
Python random getrandbits 141 0
Python random setstate 136 0
Python random getstate 178 0
Python random seed 168 1
Python random Introduction 175 0

Comments