Tuple comprehension

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Thu Apr 11 22:25:43 EDT 2002


In article <mailman.1018539023.28081.python-list at python.org>,
brueckd at tbye.com wrote:
[snip]
>I use list comprehensions all the time, but I have trouble coming up with
>too many cases where tuple comprehensions would be useful. Maybe I'm
>missing a large set of potential uses?
[snip]

Well, at the moment I'm working with coordinates and various
aggregates (related to Anygui), and would like to have tuples as the
"base case" for those, e.g.

  foo.geometry == 10, 10, 100, 100

I might want to calculate that based on a rule where I have something
like 

  names == ['x', 'y', 'width', 'height']
  state = {'x': 10, 'y': 10, ...}

In this case, a tuple comprehension would be useful to me. Not
essential, of course, but then again, neither would a list
comprehension. It just seems natural to do

  geometry = (state[name] for name in names)

(I'm assuming that the added parentheses would signal the tuple
comprehension, just like the added brackets will signal generator
comprehension.)

> -Dave

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list