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

How to select items from list randomly in Python

In Python, you can use the random module to select items from a list randomly. Here are a few common ways to do this:

  • Select a single item randomly using random.choice():
import random

my_list = [1, 2, 3, 4, 5]
random_item = random.choice(my_list)
print(random_item)
  • Select multiple unique items randomly using 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.

  • Shuffle the list randomly using 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.

  1. Randomly pick element from list in Python:

    • Description: Randomly picking a single element from a list.
    • Code:
      import random
      
      my_list = [1, 2, 3, 4, 5]
      random_element = random.choice(my_list)
      print("Randomly Picked Element:", random_element)
      
  2. Choose random items from list Python:

    • Description: Choosing a specified number of random items from a list.
    • Code:
      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)
      
  3. Random sampling from list in Python:

    • Description: Performing random sampling from a list.
    • Code:
      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)
      
  4. Python random choice from list:

    • Description: Making a random choice from a list.
    • Code:
      import random
      
      my_list = ['apple', 'orange', 'banana', 'grape']
      random_fruit = random.choice(my_list)
      print("Randomly Chosen Fruit:", random_fruit)
      
  5. Shuffle and select random element from list in Python:

    • Description: Shuffling a list and selecting a random element.
    • Code:
      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)
      
  6. Randomly select multiple items from list Python:

    • Description: Randomly selecting multiple items from a list.
    • Code:
      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)
      
  7. Select n random elements from list in Python:

    • Description: Selecting a specified number of random elements from a list.
    • Code:
      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)
      
  8. Python random sampling without replacement from list:

    • Description: Performing random sampling without replacement from a list.
    • Code:
      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)
      
  9. How to randomly pick elements with probability from list in Python:

    • Description: Randomly picking elements from a list with specified probabilities.
    • Code:
      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)