Hot on the heels of list comprehension and the proposed (planned, even?) dict comprehension, tuple comprehension may soon (?) join Python's syntax. There is some (sporadic) debate in the Python community over whether or not tuples need this, but it seems like the next logical step, at least for uniformity's sake. Ignoring some syntactical worries (parentheses being rather necessary to expressions, and therefore picked up rather quickly in parsing), it should go something on the lines of:

(f(x) for x in some_list if some_condition)

which evaluates the same as

tuple([f(x) for x in some_list if some_condition])

which is how you'd do things nowadays (which just good ol' list comprehension).

As tuples are more often type heterogenous, acting more as records than lists or arrays of objects, mapping and filtering are less important operations for them (than they are for containers such as list and dict). Still, tuple comprehension is sure to come in handy. For one thing, if you've got a list of mixed-type records, one of the first things you might want to find out is what types are there: (type(x) for x in my_tuple).

Log in or registerto write something here or to contact authors.