In this tutorial we will be focusing on how to select random elements from python list. We will be trying out different ways to subset the list or select the random elements of the list in python. one of them being with repetition using sample() method and other is to select random elements without repetition using choice() function.
- Select Random Elements from List in python using sample() function.
- Select Random elements from list with repetition using choices() function
- Select single Random element from list using choice() function
- Select Random elements from list and maintain the same list every time using seed() function.
- Select Random elements from list without repetition using choice() function
Selecting Random Elements from List in python using sample() function :
We will be selecting the random elements from list in python using sample() function. Sample() function takes the list and the number of elements in the subset list. In the below example we will be creating sample subset of 3 elements.
# importing the required module import random # list of items List = [1,1,4,3,2,4,1,2,3,4] # using the sample() method randomList = random.sample(List, 3) print(randomList)
so, the output will be,
output:
Using seed() function :
# importing the required module import random # list of items List = [1,1,4,3,2,4,1,2,3,4] ## seed() function random.seed(4) # using the sample() method randomList = random.sample(List, 3) print(randomList)
Every time the same list [3,2,2] will be repeated as we have used the seed() function.so, the output will be,
output:
Select Random Elements from List in python with repetition using choice() function :
We will be selecting the random elements from list in python using choice() function. choice() function takes the list and the number of elements required, In the below example we will be creating sample subset of 3 elements.
# Select Random elements from list with repetition using choices() function import random List = [1,1,4,3,2,4,1,2,3,4] # using the choices() method on the # given dataset randomList = random.choices(List, k = 3) print(randomList)
so, the output will be,
output:
Select single Random Element from List in python using choice() function :
We will be selecting a single random element from list in python using choice() function. When we pass only the list to the choice() function without any numbers to be subsetted as argument unlike previous example, it will result in selecting a single random element.
# Select one Random element from list using choice() function import random List = [1,1,4,3,2,4,1,2,3,4] # using the choice() method on the # given dataset randomList = random.choice(List) print(randomList)
so, the output will be,
output:
Select Random elements from list in python without repetition using choice() function:
We will be selecting random element from list in python using choice() function and we will be appending the results as long as the required subset is obtained or as long as we get all the unique values as shown below, it will result in selecting random elements without repetition as shown below
import random items = [1,1,4,3,2,4,1,2,3,4] tab= [] number_of_random_elements=3 if number_of_random_elements <= len(set(items)): while len(tab) < number_of_random_elements: item = random.choice(items) if item not in tab: tab.append(item) else: while len(tab) < len(set(items)): item = random.choice(items) if item not in tab: tab.append(item) print(tab)
so, the output will be,
output:
Get the same sample everytime using random.seed():
random.seed() function will make sure that when refreshed or rerun every time, It will result in the same list. In the below code we have shuffled and generated 3 list. Each time this 3 list will be repeated when we use the same argument inside seed() function as shown below
import random numbers = [1,1,4,3,2,4,1,2,3,4] print("Original list: ", numbers) # shuffle 3 times for i in range(3): # seed PRNG random.seed(4) # shuffle list random.shuffle(numbers,4) print("result list ", numbers)
so, the output will be,
output:
Original list: [1, 1, 4, 3, 2, 4, 1, 2, 3, 4]
result list [3, 4, 1, 2, 1, 4, 4, 1, 2, 3]
result list [2, 1, 3, 1, 4, 3, 4, 4, 1, 2]
result list [1, 3, 2, 4, 4, 2, 3, 1, 4, 1]