user96139
0
Q:

choose random index from list python

random.choice(name of list)
16
import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
11
import random  # Don't forget to install it first with pip install random

ahahfunny = ['ahah ', 'copy-', 'paste ', 'stack overflow',' go ', 'brrr']
#  the biggest index in this list above is 5 (0 being 1 in programming)

print(ahahfunny[random.randint(0, 5)]  # I like this way,
      
print(random.choice(ahahfunny)) # But this way is WAY thiccer...
4
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

New to Communities?

Join the community