1

Is there a way to do something like this in python, where if a value is equal to one of a set of values, the if statement returns true (without using or and then replicating the first expression and the condition)?

li1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
li2 = []
for n in li1:
    if n == 2, 3, 5, 8:
        li2.append(n)

This was an example without list comprehension but I want to have something like this with list comprehension, considering the whole point of it is to make code shorter.

li = [int + 1 for int in range(10) if int == 2, 3, 5, 8]

But I haven't found anything that could do this. I know you can use or and do something like this,

li = [int + 1 for int in range(10) if int + 1 == 2 or int + 1 == 3 or int + 1 == 5 or int + 1 == 8]

but it just seems lengthy. I've tried a lot of things like using lists, tuples, range(), or (without copying the first expression and the condition), etc. It either just says invalid syntax or doesn't do the right thing. I don't really know how to phrase the question, so I can't use a google search.

If anyone knows whether or not this can be done, thank you in advance! :)

TeaCoast
  • 352
  • 3
  • 12

2 Answers2

3

There are multiple ways to do that.
One would be to use if int in [2, 3, 5, 8]. Basically this: li = [int + 1 for int in range(10) if int in [2, 3, 5, 8]]

Or of course just this: li = [int + 1 for int in [2, 3, 5, 8]]

You can also use lambdas, if you want to have a more complex condition:
li = [int + 1 for int in range(10) if (lambda x: x % 3 == 0)(int)]
You would not need a lambda for that, but this tests if your int is a multiple of three.

Some thought about lambdas: Here the lambda does not really make sense as you could just write int % 3 == 0 without the lambda. I don't want to go into detail about lambdas, as this would be out of scope. So if you want some great examples on how/when to use them, have a look at this answer.

This if n == 2, 3, 5, 8 on the other hand is not valid python.

Additional:
int is a built-in which should not be used as a variable name. Even if it is syntactically correct.

toydarian
  • 4,246
  • 5
  • 23
  • 35
  • 1
    `int` is an important built-in, it shouldn't be used as a variable name. A lambda defined and immediately called in a loop makes little sense. – VPfB Aug 21 '20 at 07:10
  • 1
    @VPfB you are right, you *should* not use `int` as a variable name and I would not, but it is syntactically correct. Yes, the `lambda` there does not make to much sense, it is more of an example. – toydarian Aug 21 '20 at 07:25
  • 1
    @toydarianThe OP is apparently in the learning phase. In my opinion he should not learn to use lambdas that way. It would make sense if it were defined once before the loop and used repeatedly in the loop. – VPfB Aug 21 '20 at 07:57
  • 1
    @VPfB probably right, I added some lines on this topic. – toydarian Aug 21 '20 at 08:19
  • @toydarian I was thinking that because int is a class, it would basically be an if statement without using if, that it would take all the integers from the list. Of course, I didn't really need that considering it was from a range. I guess I would need to add `if str(n).isdigit()` to do that? – TeaCoast Aug 21 '20 at 19:20
  • Sorry, I don't understand that one. In general, please do not ask two question in one post. – toydarian Aug 24 '20 at 05:32
1
li = [int + 1 for int in range(10) if int + 1 in [2, 3, 5, 8]]
Timbolt
  • 196
  • 6