isiter builtin

Terry Reedy tjreedy at udel.edu
Sun Nov 16 04:40:22 EST 2014


On 11/16/2014 2:57 AM, Garrett Berg wrote:
> (how often have you iterated over a string?)

Often enough, but perhaps more often have written functions for which a 
string is as valid an input as many other iterables.

def cross(iterable, reiterable):
     for a in iterable:
         for b in reiterable:
             yield a, b

print(list(cross('ab', 'cde')))
# [('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e')]

For me, there is no point in excluding such input and output.

(And yes, the code above could be enhanced by at least checking that 
reiterable is *not* an iterator, or by replacing the second arg, renamed 
iterable2, with list(iterable2), but this is besides the point.)

-- 
Terry Jan Reedy




More information about the Python-list mailing list