Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline for in expression evaluation

Is there a way I could inline this for loop?

already_inserted = True
for i in indexes:
    already_inserted = already_inserted and bitfield[i]
like image 421
Paul Manta Avatar asked Feb 07 '12 11:02

Paul Manta


3 Answers

already_inserted = all(bitfield[i] for i in indexes)
like image 150
Tim Pietzcker Avatar answered Oct 27 '22 20:10

Tim Pietzcker


How about:

already_inserted = all(bitfield[i] for i in indexes)
like image 24
NPE Avatar answered Oct 27 '22 19:10

NPE


all() function accepts iterable and will automatically go over all elements and apply bool to each of them. Therefore, it is sufficient to write:

already_inserted = all(bitfield)
like image 27
Samvel Avatar answered Oct 27 '22 21:10

Samvel