-1

I am making a program that will need to print a lot of hints and in order to avoid repetition and make it more interesting, I want the program to pick those hints randomly from lists that I am going to make. I also want to remove a hint after being used so the program doesn't use it again in a place that it won't need to. Can someone show an example of that on this code:

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
mkrieger1
  • 14,486
  • 4
  • 43
  • 54
RobertP05
  • 3
  • 1

2 Answers2

3

random.shuffle and foo.pop:

>>> import random
>>> foo = ['a', 'b', 'c', 'd', 'e']
>>> random.shuffle(foo)
>>> print(foo.pop())
d
>>> print(foo.pop())
e
>>> print(foo.pop())
a
>>> foo
['c', 'b']
Samwise
  • 51,883
  • 3
  • 26
  • 36
-1

You can use the pop method:

import random

foo = ['a', 'b', 'c', 'd', 'e'];
i = random.randint(0, len(foo));
bar= foo.pop(i);

print(bar);
print(foo);

Check more in Python Documentation about Data Structures: https://docs.python.org/3/tutorial/datastructures.html