Im trying to do a simple check if there is 3 specific value in a string. If there is, the statement should return nothing, instead of save.
Here is my code, but I think the syntax is wrong:
if not ('2239687' or '2238484' or '2239440') in user_id:
#The user is not admin, save the user
web_user.save()
To elaborate, I want it to test if user_id is "2239687" or "2238484" or "2239440" (and not, for example, "002239440"). If the user_id is one of those three values (and ONLY those three values), the statement should return false.
georg
216k57 gold badges325 silver badges401 bronze badges
asked Mar 26, 2013 at 8:35
Tomas Jacobsen
2,4366 gold badges38 silver badges83 bronze badges
3 Answers 3
if not any(x in user_id for x in ('2239687', '2238484', '2239440')):
#The user is not admin, save the user
web_user.save()
This checks whether none of the three string is present within user_id.
answered Mar 26, 2013 at 8:46
eumiro
214k36 gold badges307 silver badges264 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
One more option:
if not any(idx in user_id for idx in ('2239687' ,'2238484' , '2239440')):
# do something
answered Mar 26, 2013 at 8:40
Artsiom Rudzenka
29.3k5 gold badges36 silver badges53 bronze badges
4 Comments
jamylak
This should not be used in this case
Artsiom Rudzenka
@jamylak yes, maybe you are right, then just a case to know that 'any' function exists
Artsiom Rudzenka
@glglgl i know - but each ohe us can only suppose what type of comparison he need - the option here is 'any' function not a comparison type
eumiro
id is a built-in function. Don't use it as a variable name.Try like this
if user_id not in ('2239687' ,'2238484' , '2239440'):
Or
if not user_id in ('2239687' ,'2238484' , '2239440'):
answered Mar 26, 2013 at 8:36
Adem Öztaş
21.6k5 gold badges36 silver badges42 bronze badges
3 Comments
Tomas Jacobsen
This seems right, but the "save" does still gets executed me (I'm 2239687).
Adem Öztaş
@Garreth00 str(2239687) not in ('2239687' ,'2238484' , '2239440') try like this,
Artsiom Rudzenka
Correct me but i always thought that 'not user_id in ('2239687' ,'2238484' , '2239440'):' is not very pythonic?
lang-py
99922396879999?user_idcontains "2239687" (or 2238484 or 2239440) - as in "99922396879999" - or if it's equal to "2239687" (or 2238484 or 2239440)?