Python | random.sample() function
sample() is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.
Syntax : random.sample(sequence, k)
Parameters:
sequence: Can be a list, tuple, string, or set.
k: An Integer value, it specify the length of a sample.
Returns: k length new list of elements chosen from the sequence.
Code #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 = [1, 2, 3, 4, 5] print(sample(list1,3)) |
Output:
[2, 3, 5]
Code #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 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks"print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3)) |
Output:
With list: [3, 1, 2] With string: ['e', 'f', 'G', 'G'] With tuple: ['ankit', 'portal', 'geeks', 'computer'] With set: ['b', 'd', 'c']
Note: Output will be different everytime as it returns a random item.
Code #3: Raise Exception
If the sample size i.e. k is larger than the sequence size, ValueError is raised.
# Python3 program to demonstrate the # error of sample() function. import random list1 = [1, 2, 3, 4] # exception raised print(random.sample(list1, 5)) |
Output:
Traceback (most recent call last):
File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in
print(random.sample(list1, 5))
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample
raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative
Recommended Posts:
- Help function in Python
- Python map() function
- Python | int() function
- Python | hex() function
- Python | now() function
- sum() function in Python
- Python | cmp() function
- Python | dir() function
- Python | How to get function name ?
- id() function in Python
- ord() function in Python
- Python | oct() function
- Function Annotations in Python
- Python | __import__() function
- atan2() function in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



