Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching Iterators With Discard In Python

Is there any way to do what this implies:

a, b, ... = count()

The idea is to match the first two values from the (infinite, in this case) iterator, and discard the rest.

The best I can do is the clunky:

a, b = islice(count(), 0, 2)

which requires you to count the number of entries on the left hand side.

Is there a cool hack I am missing?

like image 731
andrew cooke Avatar asked Feb 18 '26 20:02

andrew cooke


1 Answers

No, there is no simple way to that unless you explicitly tell the amount of elements you want.

There has been a lot of discussion (I think in the end of last year when they were introducing the enum library) in the Python-ideas mail group about this and nobody could agree to a way of solving this problem.

For finite iterators, on Python 3 you can use:

a, b, *_ = thing()

Where the _ variable may be ignored.

like image 136
JBernardo Avatar answered Feb 20 '26 08:02

JBernardo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!