I'm searching for a "pythonic" way to assert the type of the first element in an iterable.
For example if i have the following list:
l = [(1,2,3), (4,5,6), ...]
And a function which it is passed into, i can simply do:
def foo(l):
assert isinstance(l[0], tuple)
...
Now i'm searching for something similar to this that works with iterators as well, but doesn't load the whole list into ram. I can try:
def foo(it):
assert isinstance(next(it), tuple)
...
it = iter(l)
foo(it)
but this obviously modifies the state of the iterator in the assertion.
Is there an easy way to peek at the first element of an iterator without modifying it?