Q:

python random choice from list

random.choice(name of list)
16
import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
11
import random

list = ["Item 1", "Item 2", "Item 3"]			# List
item = random.choice(list)						# Chooses from list
print(item)					# Prints choice

# From stackoverflow
# Tried and tested method
5
import random

#1.A single element
random.choice(list)

#2.Multiple elements with replacement
random.choices(list, k = 4)

#3.Multiple elements without replacement
random.sample(list, 4)
8
import random
names=['Mark', 'Sam', 'Henry']

#Set any array
random_array_item=random.choice(names)

#Declare a variable as a random choice
print(random_array_item)

#Print the random choice
#Or, if you want to arrange them in any order:
for j in range(names):
  print(random.choice(names))
5
import random
l=[1,2,3,4,5,6,7,8,9,0]
random.choice(l)
print(l);
2
import random

# with replacement = same item CAN be chosen more than once.
# without replacement = same item CANNOT be chosen more then once.

# Randomly select 2 elements from list without replacement and return a list
random.sample(list_name, 2)

# Randomly select 3 elements from list with replacement and return a list
random.choices(set_name, k=3)

# Returns 1 random element from list
random.choice(list_name)
1
import random
list = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(list, k=4)      # Choices with repetition
sampling = random.sample(list, k=4)       # Choices without repetition
0
import random
sequence = [i for i in range(20)]
subset = sample(sequence, 5) #5 is the lenth of the sample
print(subset) # prints 5 random numbers from sequence (without replacement)
-1

New to Communities?

Join the community