Skip to content
Advertisement

Tag: iterator

Iterate over n successive elements of list (with overlapping)

The itertools python module implements some basic building blocks for iterators. As they say, “they form an iterator algebra”. I was expecting, but I could not find a succinctly way of doing the following iteration using the module. Given a list of ordered real numbers, for example … return a new list (or just iterate) grouping by some n value,

custom dict that allows delete during iteration

UPDATED based on Lennart Regebro’s answer Suppose you iterate through a dictionary, and sometimes need to delete an element. The following is very efficient: The only overhead here is building the list of keys to remove; unless it grows large compared to the dictionary size, it’s not an issue. However, this approach requires some extra coding, so it’s not very

Is there a Java equivalent of Python’s ‘enumerate’ function?

In Python, the enumerate function allows you to iterate over a sequence of (index, value) pairs. For example: Is there any way of doing this in Java? Answer For collections that implement the List interface, you can call the listIterator() method to get a ListIterator. The iterator has (amongst others) two methods – nextIndex(), to get the index; and next(),

How to convert an iterable to a stream?

If I’ve got an iterable containing strings, is there a simple way to turn it into a stream? I want to do something like this: Answer Here’s my streaming iterator an experimental branch of urllib3 supporting streaming chunked request via iterables: Source with context: https://github.com/shazow/urllib3/blob/filepost-stream/urllib3/filepost.py#L23 Related unit tests: https://github.com/shazow/urllib3/blob/filepost-stream/test/test_filepost.py#L9 Alas this code hasn’t made it into the stable branch yet

Is enumerate in python lazy?

I’d like to know what happens when I pass the result of a generator function to python’s enumerate(). Example: Is the enumeration iterated lazily, or does it slurp everything into the <enumerate object> first? I’m 99.999% sure it’s lazy, so can I treat it exactly the same as the generator function, or do I need to watch out for anything?

Get the first item from an iterable that matches a condition

I would like to get the first item from a list matching a condition. It’s important that the resulting method not process the entire list, which could be quite large. For example, the following function is adequate: This function could be used something like this: However, I can’t think of a good built-in / one-liner to let me do this.

When is not a good time to use python generators?

This is rather the inverse of What can you use Python generator functions for?: python generators, generator expressions, and the itertools module are some of my favorite features of python these days. They’re especially useful when setting up chains of operations to perform on a big pile of data–I often use them when processing DSV files. So when is it

Advertisement