Is there any way to peek at the stdin buffer?

Portably, you can get the next character in the input stream with getchar() and then push it back with ungetc(), which results in a state as if the character wasn’t removed from the stream. The ungetc function pushes the character specified by c (converted to an unsigned char) back onto the input stream pointed to … Read more

How to look ahead one element (peek) in a Python generator?

For sake of completeness, the more-itertools package (which should probably be part of any Python programmer’s toolbox) includes a peekable wrapper that implements this behavior. As the code example in the documentation shows: >>> p = peekable([‘a’, ‘b’]) >>> p.peek() ‘a’ >>> next(p) ‘a’ However, it’s often possible to rewrite code that would use this … Read more