Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply an operator sequentially to all items in a list?

I have an arbitrarily sized list:

l = [1, 2, 3, 4, 5, ...]

I want to apply the pipe operator to all items in the list, sequentially, like this:

1 | 2 | 3 | 4 | 5 # = 7

I know there is a function in the stdlib that does this, and that this question is probably a duplicate, but I don’t recall the function and can’t find answer pointing me to it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use functools.reduce in conjunction with operator.or_.

from functools import reduce
import operator
l = [1, 2, 3, 4, 5]
res = reduce(operator.or_, l)
print(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading