I have a list of words named words
and I would like to generate a list of 100 three-words elements named pwds
.
I want these 3 words to be randomly picked from the words
list for each element of the pwds
list, and I'd like to use list comprehension to do this.
This solution works :
pwds = [random.choice(words)+' '+random.choice(words)+' '+random.choice(words) for i in range(0,100)]
It generates a list that looks like : ['correct horse battery', 'staple peach peach', ...]
But I was looking for a way that prevents repetiting 3 times random.choice(words)
, so I tried this :
pwds = [(3*(random.choice(words)+' ')).strip() for i in range(0,100)]
But unfortunately, this solution makes each element having the same word three times (for example : ['horse horse horse', 'staple staple staple', ...]
), which is expected to happend.
Do you know a way to pick 3 random words without repetition (EDIT : by "repetition", I mean the code repetition, not the random words repetition) ?
EDIT : My question is different than the one it has been marked as duplicate of because I'm looking for using list comprehension here. I know how I could generate different numbers, I'm just looking for specific way to do it.