how to extract an implicit dict expression (with statement return)

Benjamin Kaplan benjamin.kaplan at case.edu
Thu Sep 9 17:28:05 CEST 2010


On Thu, Sep 9, 2010 at 11:20 AM, Fritz Loseries <fritz at loseries.info> wrote:
> Hi,
>
> I do not know how to subject my problem in a better way.
>
> I have the following statement:
>
>    return [ dict(x1 = elem.x1, x2 = elem.x2, x3 = elem.x3,)
>                for elem in method(in_values)
>              ]
>
> How can I transform it to an explicit description:
>
>    result = ...
>    return result
>
> Could not found a description for the here used form of for and what
> [...] means.
>
> Need this for debugging.
> Thanks for any help.
>
> Regards,
>
> Fritz


[ ] defines a list literal
>>> [1,2,3]
[1, 2, 3]
>>> type(_)
<type 'list'>

This particular syntax [ ____ for ___ in ___] is a list comprehension.
It's creating an inline for-loop to generate a list. You can just lift
the whole expression for the assignment.

result = [ dict(x1 = elem.x1, x2 = elem.x2, x3 = elem.x3,)
                for elem in method(in_values)
              ]
return result

>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list