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?
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]
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?
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]