It is easy to convert an entire iterator sequence into a list using list(iterator)
, but what is the best/fastest way to directly create a sublist from an iterator without first creating the entire list, i.e. how to best create list(iterator)[m:n]
without first creating the entire list?
It seems obvious that it should not* (at least not always) be possible to do so directly for m > 0
, but it should be for n
less than the length of the sequence. [p for i,p in zip(range(n), iterator)]
comes to mind, but is that the best way?
The context is simple: Creating the entire list would cause a RAM overflow, so it needs to be broken down. So how do you do this efficiently and/or python-ic-ly?
*The list comprehension I mentioned could obviously be used for m > 0
by calling next(iterator)
m
times prior to execution, but I don't enjoy the lack of python-ness here.