I am trying to retrieve a random string from python list to build a word guess challenge. I deployed anaconda environment on Alibaba Cloud ECS Instance.
I often use the following method to retrieve a random string from the list.
Let's say
WordStack=['A','B','C','D']
print(WordStack[random.randint(len(WordStack))])
Is there any optimized way or build-in funtion to do that? Due to a large number of words, it takes some time to give the result.
Take a look at
random.choice
which does exactly what you need. For your case, it would look like this:Having said that, I wouldn't expect it to make such a big difference on the speed of the process. But If I find the time I will test it.