Fix Python – If range() is a generator in Python 3.3, why can I not call next() on a range?

Perhaps I’ve fallen victim to misinformation on the web, but I think it’s more likely just that I’ve misunderstood something. Based on what I’ve learned so far, range() is a generator, and generators can be used as iterators. However, this code:
myrange = range(10)

gives me this error:
TypeError: ‘range’ object is not an it….

Fix Python – Iterate over the lines of a string

I have a multi-line string defined like this:
foo = “””
this is
a multi-line string.

This string we used as test-input for a parser I am writing. The parser-function receives a file-object as input and iterates over it. It does also call the next() method directly to skip lines, so I really need an iterator as input, not an iterable.
I need ….

Fix Python – How to print a generator expression?

In the Python shell, if I enter a list comprehension such as:
>>> [x for x in string.letters if x in [y for y in “BigMan on campus”]]

I get a nicely printed result:
[‘a’, ‘c’, ‘g’, ‘i’, ‘m’, ‘n’, ‘o’, ‘p’, ‘s’, ‘u’, ‘B’, ‘M’]

Same for a dictionary comprehension:
>>> {x:x*2 for x in range(1,10)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16….

Fix Python – Circular list iterator in Python

I need to iterate over a circular list, possibly many times, each time starting with the last visited item.
The use case is a connection pool. A client asks for connection, an iterator checks if pointed-to connection is available and returns it, otherwise loops until it finds one that is available.
How can I do this neatly in Python?

If you inste….

Fix Python – Update row values where certain condition is met in pandas

Say I have the following dataframe:

What is the most efficient way to update the values of the columns feat and another_feat where the stream is number 2?
Is this it?
for index, row in df.iterrows():
if df1.loc[index,’stream’] == 2:
# do something

What to do if I have more than a 100 columns? I don’t want to explicitly name th….