Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get new current list randomly but the randomly generated list should have TRUE in it,
eg.['TRUE','FALSE' , 'FALSE', 'FALSE',] or ['FALSE' , 'FALSE', 'FALSE','TRUE']

Python
import random

list2 = ['TRUE','FALSE' , 'FALSE', 'FALSE', 'FALSE','FALSE','FALSE' ]
current = []

def randompicker():
    for i in range(4):
        current.append(random.choice(list2))
      
randompicker()


s = current


What I have tried:

Python
for x in s:
    if 'TRUE' in x:
        print (x)
Posted
Updated 8-May-19 20:15pm
v2
Comments
Richard MacCutchan 9-May-19 3:43am    
You cannot use the expression if 'TRUE' in x, as x is not a list. It should be just
if 'TRUE' in s:

1 solution

Quote:


You can use this syntax:

Python
if myItem in list:
    # do something


Also, inverse operator:

Python
if myItem not in list:
    # do something


It's work fine for lists, tuples, sets and dicts (check keys).

Note that this is an O(n) operation in lists and tuples, but an O(1) operation in sets and dicts.


source: python - Is there a short contains function for lists? - Stack Overflow[^]


The same is stated here: Python : How to Check if an item exists in list ? | Search by Value or Condition – thispointer.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900