Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, you can use the random
module to select items from a list randomly. Here are a few common ways to do this:
random.choice()
:import random my_list = [1, 2, 3, 4, 5] random_item = random.choice(my_list) print(random_item)
random.sample()
:import random my_list = [1, 2, 3, 4, 5] num_items = 3 random_items = random.sample(my_list, num_items) print(random_items)
Note that random.sample()
raises a ValueError
if the specified sample size is larger than the size of the input list.
random.shuffle()
:import random my_list = [1, 2, 3, 4, 5] random.shuffle(my_list) print(my_list)
After shuffling the list, you can take the first n
items, for example, as your random selection. Note that this method modifies the input list in place. If you want to keep the original list unchanged, make a copy before shuffling.
import random my_list = [1, 2, 3, 4, 5] shuffled_list = my_list.copy() random.shuffle(shuffled_list) num_items = 3 random_items = shuffled_list[:num_items] print(random_items)
Choose the method that best suits your needs depending on whether you need a single random item, multiple unique random items, or a shuffled list.
Randomly pick element from list in Python:
import random my_list = [1, 2, 3, 4, 5] random_element = random.choice(my_list) print("Randomly Picked Element:", random_element)
Choose random items from list Python:
import random my_list = [1, 2, 3, 4, 5] num_items = 3 random_items = random.sample(my_list, num_items) print("Randomly Chosen Items:", random_items)
Random sampling from list in Python:
import random my_list = [1, 2, 3, 4, 5] sample_size = 2 random_sample = random.sample(my_list, sample_size) print("Random Sample:", random_sample)
Python random choice from list:
import random my_list = ['apple', 'orange', 'banana', 'grape'] random_fruit = random.choice(my_list) print("Randomly Chosen Fruit:", random_fruit)
Shuffle and select random element from list in Python:
import random my_list = [1, 2, 3, 4, 5] random.shuffle(my_list) random_element = my_list[0] print("Shuffled List:", my_list) print("Randomly Selected Element:", random_element)
Randomly select multiple items from list Python:
import random my_list = ['a', 'b', 'c', 'd', 'e'] num_items = 3 random_selection = random.sample(my_list, num_items) print("Randomly Selected Items:", random_selection)
Select n random elements from list in Python:
import random my_list = [10, 20, 30, 40, 50] num_elements = 2 random_elements = random.sample(my_list, num_elements) print("Randomly Selected Elements:", random_elements)
Python random sampling without replacement from list:
import random my_list = ['red', 'green', 'blue', 'yellow'] sample_size = 2 random_sample = random.choices(my_list, k=sample_size) print("Random Sample without Replacement:", random_sample)
How to randomly pick elements with probability from list in Python:
import random my_list = ['apple', 'orange', 'banana', 'grape'] probabilities = [0.3, 0.2, 0.1, 0.4] random_fruit = random.choices(my_list, weights=probabilities, k=1)[0] print("Randomly Chosen Fruit with Probability:", random_fruit)