[Tutor] Dictionary of dictionaries issue

Tim Golden mail at timgolden.me.uk
Thu Nov 13 16:34:50 CET 2008


Kent Johnson wrote:
> On Thu, Nov 13, 2008 at 10:01 AM, A.T.Hofkamp <a.t.hofkamp at tue.nl> wrote:
>>>>> d3 = dict(( (rv, dict.fromkeys(c)) for rv in r ))
> 
> You don't need the double parentheses, this works just as well:
> d3 = dict( (rv, dict.fromkeys(c)) for rv in r )
> 
> A generator expression just has to be in parentheses, it's OK if the
> parens also have other meaning syntactically; in this case indicating
> a function call.

... as long as it's the only argument to the function call.
Otherwise you'll get a syntax error[*]. But you'll find this
out pretty quickly when you try anyway!

I realise that, strictly speaking, Kent's explanation already says this
if you read "in parentheses" as meaning "the only thing in paren..."
but just in case :)

TJG


<dump>
>>> sum (x*x for x in range (10), 5)
  File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument
>>>
>>>
</dump>


More information about the Tutor mailing list