Best way to check that you are at the beginning (the end) of an iterable?

Laurent laurent.payot at gmail.com
Wed Sep 7 21:05:17 EDT 2011


> I don't think this question is meaningful. There are basically two
> fundamental types of iterables, sequences and iterators.
> 
> Sequences have random access and a length, so if the "start" and "end" of
> the sequence is important to you, just use indexing:
> 
> beginning = sequence[0]
> end = sequence[-1]
> for i, x in enumerate(sequence):
>     if i == 0: print("at the beginning")
>     elif i == len(sequence)-1: print("at the end")
>     print(x)
> 
> 
> Iterators don't have random access, and in general they don't have a
> beginning or an end. There may not be any internal sequence to speak of:
> the iterator might be getting data from a hardware device that provides
> values continuously, or some other series of values without a well-defined
> beginning or end. 

Maybe I should have said "best way to check that you didn't start the iteration process yet" but you see what I mean.

Well I guess I have to unlearn my bad lisp/scheme habits...




More information about the Python-list mailing list