Python Forum
How to reduce the following code to run in sequence? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to reduce the following code to run in sequence? (/thread-27938.html)



How to reduce the following code to run in sequence? - Giggel - Jun-28-2020

Hello all,
I would like to reduce the following python code.
I have one list:
a=[one, two, three, four]
And the following statement s are all true

if a[0] in b:
     print(a[1])
if a[1] in b:
     print(a[2])
if a[2] in b:
     print(a[3])
if a[3] in b:
     print(a[0])
How can I reduce this to two lines of code and still to run in sequence? It will permit list a to be of "n" length and I will not have to add them individually.


RE: How to reduce the following code to run in sequence? - menator01 - Jun-28-2020

It would help to know what b is.


RE: How to reduce the following code to run in sequence? - Giggel - Jun-28-2020

b=["five","four","three","two","one"]
I've said is true.


RE: How to reduce the following code to run in sequence? - menator01 - Jun-28-2020

Here is a couple of examples
a = ['one','two','three','four']
b = ['one','two','three']
# First
for i in range(len(a)):
    if a[i] in b:
        print(f'{a[i]} is in b')
    else:
        print(f'{a[i]} in not in b')

#Second
[print(a[i]) for i in range(len(a)) if a[i] in b]
Output:
1st example one is in b two is in b three is in b four in not in b 2nd example one two three



RE: How to reduce the following code to run in sequence? - Giggel - Jun-28-2020

Thank you!