I’m using Python 2.7.
Let’s say I have a list like so:
string_list = ['hello', 'apple', 'green', 'paint', 'sting']
Where each string in the list is the same length.
I want to create a generator that would be doing something like the following code:
for i in xrange(len(string_list)):
my_gen = (ch for a_string[i] in string_list)
So the first run, my_gen would have ‘h’, ‘a’, ‘g’, ‘p’, s’. The next run it would have ‘e’, ‘p’, ‘r’, ‘a’, ‘t’.
Just use the built-in function
zip–like in
zip is a built-in that does just that: combine one element of each iterable in a tuple, for each iteration.
Running the above example, you have: