I want to perform a conditional branch using a list in Python's for loop statement.

Asked 2 years ago, Updated 2 years ago, 19 views

We would like to perform a conditional branch using a list in the python for loop statement.Specifically,

 li = [2,4,6,9]
for i in range (100):
    if i!=2 and i!=4 and i!=6 and i!=9:

"I would like to express ""2,"" ""4,"" ""6,"" and ""9"" in the if statement above using the list li."
It doesn't have to be written like above
Thank you for your cooperation.

python

2022-09-30 16:49

3 Answers


You can use not in .

The operators in and not in look at membership, where x is s
The element in is true, otherwise false, and x notins returns xins negation.

in not in li


2022-09-30 16:49

If you want to write in the for sentence, the iterator is better.

for in (x for x in range(100) if x not in li):

Or

for i filter (lambdax:x not in li, range(100)):


2022-09-30 16:49

A1

 li = [2,4,6,9]
for i in range (100):
    if not in li:
        # Implement what you want to do here

A2

 li = [2,4,6,9]
for in [ x for x in range(100) if x not in li ]:
    # Implement what you want to do here


2022-09-30 16:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.