How to Select Random Element from a List in Python

Here are 8 ways to select random element from a list in Python:

  1. Using random.choice() method
  2. Using random.randrange() method
  3. Using random.randint() method
  4. Using random.random() method
  5. Using random.sample() method
  6. Using random.choices() method
  7. Select k random value from a list
  8. Using numpy.random.choice() method

Method 1: Using the random.choice() method

To select a random element from a list in Python, you can use the “random.choice()” method. The random choice() method returns a random element from the non-empty sequence-like list.

Example

To select a random element from the list, use the “random.choice()” method and pass the list as an argument.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

print(random.choice(data))

Output

astral

You can see that it selects one random element from the list and returns it. If you rerun the above code, then every time, it will return a different element.

➜ python3 app.py
infy
➜ python3 app.py
tcs
➜ python3 app.py
affle

Method 2: Using the random.randrange() method

The random.randrange() method is used to generate a random number in a given range, and we can specify the range to be 0 to the list length, get the index, and then the corresponding value.

Example

import random 

data = ["infy", "tcs", "affle", "dixon", "astral"]

rand_idx = random.randrange(len(data))
random_num = data[rand_idx]

print("Random selected element is : " + random_num)

Output

Random selected element is : astral

Method 3: Using the random.randint() method

The random.randint() method is used to generate the random number. Also, this can be used to generate any number in a range, and then using that number, we can find the value at the corresponding index.

Example

import random 

data = [11, 19, 18, 21, 46]

rand_idx = random.randint(0, len(data) - 1)
random_num = data[rand_idx]

print("Random selected number is : " + str(random_num))

Output

Random selected number is : 11

Method 4: Using the random.random() method

The random.random() method is used to generate the floating-point numbers in the range of 0 to 1.

Example

import random 

data = [11, 19, 18, 21, 46]

rand_idx = int(random.random() * len(data))
random_num = data[rand_idx]

print("Random selected number is : " + str(random_num))

Output

Random selected number is : 18

Method 5: Selecting multiple random elements from a list in Python

To select multiple random elements from a Python list, use the “random.sample()” method. The random.sample() method returns a particular length of the list chosen from a sequence.

Example

To use the sample() method in Python, import the random module in your program.

import random

We need three random elements from the list, and then we will pass three as the second argument, which suggests the number of elements you need in the list.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

print(random.sample(data, 3))

Output

['infy', 'affle', 'astral']

Method 6: Using the random.choices() method

The random.choices() method is “used to select numerous elements from a list or a single element from a specific sequence.”

Example

import random 

data = [11, 19, 18, 21, 46]

print("Random selected number is : ", random.choices(data, k = 3))

Output

Random selected number is : [46, 46, 21]

Method 7: Select k random value from a list

import random


def select_random_Ns(l, k):
  random.shuffle(l)
  result = []
  for i in range(0, len(l), k):
    result.append(l[i:i + k])
  return result


lst = ['D', 'A', 'T', 'A', 'B', 'A', 'S', 'E']

print(select_random_Ns(lst, 4))

Output

[['E', 'A', 'A', 'B'], ['T', 'D', 'A', 'S']]

Method 8: Using the numpy.random.choice() method

import numpy as np

data = [21, 19, 18, 46]

arr = np.array(data)

random_num = np.random.choice(arr)

print("Random selected number is : " + str(random_num))

Output

Random selected number is : 18

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.