[IronPython] Unpacking

Vernon Cole vernondcole at gmail.com
Wed Sep 8 16:13:32 CEST 2010


If you bring up Python in interactive mode, and type "import this", some
sage advice will appear:
v v v v v
C:\Users\vernon>ipy
C:\Users\vernon>"c:\program files\IronPython 2.7\ipy.exe"
IronPython 2.7 Alpha 1 (2.7.0.1) on .NET 4.0.30319.1
>>> import this

Explicit is better than implicit.
Simple is better than complex.
Readability counts.
^ ^ ^ ^

In that spirit, may I suggest the following simple, readable code?
<code>
ml = ['name1','x1','y1','name2','x2','y2']
i = 0
d = {}
while True:
    try:
        d[ml[i]] = (ml[i+1],ml[i+2])
    except IndexError:
        break
    i += 3
print repr(d)
</code>

On Wed, Sep 8, 2010 at 5:29 AM, Iivari Mokelainen <iivari at mokelainen.com>wrote:

>  I need to unpack a list of
>    [name, x, y, name, x, y]
> to a dictionary
>    {name : (x,y), name:(x,y)}
> how would one do that?
>
> Also, is it ok to ask such newbie questions here, or is there a better
> place for that?
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100908/f1b6631f/attachment.html>


More information about the Ironpython-users mailing list