Skip to content Skip to sidebar Skip to footer

Index Of A Random Item From A List

I have following list: cnames = [' green ', ' blue ', ' yellow ', ' gray ', ' pink ', ' orange ', 'purple ', ' red ', 'brown '] How do I get 6 random and unique indexes representi

Solution 1:

The random module should help.

import randomrandom.sample(cnames, 6)   #random.sample returns unique list of random choice. 

random.sample(range(len(cnames)), 6)#to get random int representing a number between 0andlen(cnames)

Output:

[' gray ', ' pink ', 'purple ', ' blue ', ' orange ', ' green ']

Post a Comment for "Index Of A Random Item From A List"