How to remove values ​in indexes other than a specific index?

Question:

list= [True,False,False,False,False,False]
    
a= random.randint(0,5)

I have this code and I want to remove the indexes other than list[a] index.

How can I do it?

Is there any spesific function or method for this?

Asked By: Tuğrul765

||

Answers:

boolist = [True, False, False, False, False, False]   
a = random.randint(0, 5)
boolist = [boolist[a]]

(Never override bultins)


I’m not quite sure why you need a list of only one item, but for your particular case there’s a couple of handy functions in random module:

>>> random.choice(boolist)
False
# if you insist on a list
>>> random.choices(boolist, k=1)
[False]
Answered By: Klas Š.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.