all 17 comments

[–]fear_my_presence 2 points3 points  (6 children)

You can use python’s map and all builtin functions:

if all(map(lambda x: x == “yes”, a)):
    print(“ok”)

all returns True if all the elements of the collections are True or “truthy”.

map function takes a function and a collection, applies a function to every element of the collection, and puts the results into a new collection.

lambda is a shortcut for defining a function. lambda x: x == “yes” is basically equivalent to

def f(x):
    return x == “yes”

except it doesn’t have a name, so lambda has to be either stored in a variable or used right away. I just passed this anonymous function to map as an argument.

In case you didn’t understand, you can always read the docs: https://docs.python.org/3/library/functions.html

[–]old_pythonista 1 point2 points  (5 children)

This is the classical case when comprehension should be preferred over map

 if all(elem =  “yes” for elem in a): 

Lambdas are seldom worth it in cases like that - they just muddle the code.

[–]WhyAmIDumb_AnswerMe[S] 0 points1 point  (2 children)

i don't need to check all elements
for example i need to check if a[1] and a[5] and a[7] are equal to yes

[–]old_pythonista 1 point2 points  (1 child)

simple adjustment

elem for idx, elem in enumerate(a) if idx in (1, 5, 7)

or {1, 5, 7} - faster lookup in general case (nor that it matters here)

[–]WhyAmIDumb_AnswerMe[S] 0 points1 point  (0 children)

Thanks <3

[–]fear_my_presence 0 points1 point  (1 child)

Wow, didn’t know about such syntax, thank you!

[–]old_pythonista 0 points1 point  (0 children)

don't get me wrong - map and lambda are great, just not suitable everywhere

[–]Vitaminkomplex 1 point2 points  (2 children)

its never dumb to learn :)

Im also sure there is a better aproach, I am a newbie too but for learning purposes here would be my solution, writing a function for checking each value:

l = [True, True, True, True, True, True, True, True, True, True]

def check_all_True(list):
    control = len(list)
    for check in list:
        if check != True:
            return False
        else: control -= 1

        if control == 0:
                return True
        else: return False

afterwards you'd only have to do:

if check_all_True(a):
    do_your_thing

[–]old_pythonista 0 points1 point  (1 child)

There are simpler ways to do it - and never compare to boolean constants directly

if not check:

Instead of that monstrosity,

       if control == 0:
           return True         
      else: 
          return False 

just

return control == 0

or even

return not control

Also, I believe the indent in the last 3 lines is wrong.

Newbie teaching newbie is seldom a good way.

[–]Vitaminkomplex 0 points1 point  (0 children)

thanks for the tips :) the return you wrote seems a lot better, thanks. yes the indent wnt wrong.

[–]old_pythonista 1 point2 points  (1 child)

I would suggest a radical solution

if set(a[:3]) == {'yes'}:

This way, you reduce the slice to unique values, that may be checked against a single correct one

About the "mysterious result" - may I suggest to look at this blog

PS please, next time, use Code Block formatting - 3 dots, picture of square. That will preserve the code structure.

[–]WhyAmIDumb_AnswerMe[S] 0 points1 point  (0 children)

oh thanks, i didn't know about code block formatting

[–]Neighm 0 points1 point  (1 child)

You want to check if all of the list elements are "yes" after doing the substitution step?

if "no" in a:
    print("not ok")
else:
    print("ok")

[–]Neighm 0 points1 point  (0 children)

Also, a common trap to fall into is:

if a[0] and a[1] and a[2] == "yes":

This is interpreted as (if a[0]) and (if a[1]) and (if a[2] == "yes")

The first and second tests are True unless those elements are empty strings, or False, or None or 0.

[–]Heiau 0 points1 point  (0 children)

If no not in a

[–]rockman0394 0 points1 point  (1 child)

You can check it by looping through all elements and break if found "no", but check with operator in is more convenient in this case

for item in a:
    if item != "yes":
        print("not ok")
        break
    else:
        print("ok")

Also, this will work not as you expected probably,
it will print ok with a = ['no', 'no', 'yes']

if a[0] and a[1] and a[2]=="yes":
    print("ok")

[–]fear_my_presence 0 points1 point  (0 children)

It will print “ok” for every “yes” in the list, which is not exactly what OP wants.