Iterables

“Remember, an Iterable is just an object capable of returning its members one at a time.” - Jeff Knupp

https://jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/

enumerate

Adds a counter to an iterable and returns as an enumerate object. When converted to a list, it’s a list of tuples, each containg an item and its number.

Example:

>>> names
['Julian', 'Bob', 'PyBites', 'Dante', 'Martin', 'Rodolfo']

>>> enumerate(names)
<enumerate object at 0x7f77b7fa3bd0>

>>> [name for name in enumerate(names)]
[(0, 'Julian'), (1, 'Bob'), (2, 'PyBites'), (3, 'Dante'), (4, 'Martin'), (5, 'Rodolfo')]

Generators

The following creates a generator object even though it uses return instead of yield, because of the use of parenthesis:

::
with open(tempfile) as f:
return (line.split(‘,’)[0].lower()
for line in f.readlines() if line.strip().endswith(‘True’))

The code inside the parenthesis IS a generator. Confirmation:

>>> with open(tempfile) as f:
...     data = (line.split(',')[0].lower()
...                 for line in f.readlines()
...                 if line.strip().endswith('True'))
...
>>> type(data)
<class 'generator'>