[Tutor] Reversed dictionary returned by default

Joel Goldstick joel.goldstick at gmail.com
Fri Mar 1 21:02:06 CET 2013


On Fri, Mar 1, 2013 at 2:48 PM, Huperetes <jhuperetes at gmail.com> wrote:

> I am getting the following for my installation.
>
> Why is this happening, and how do I get it to work properly (returning
> element 0 - n, versus n - 0)?
>

This is happening because dictionaries are not ordered.  The fact that it
happens to look like it is reverse order isn't guaranteed either.  You use
the key to access the value.  To learn more about this google 'why python
dicts are unordered'

>
> Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa",
> "pwd":"secret"}
> >>> ["%s=%s" % (k, v) for k, v in params.items()]
>
> ['pwd=secret', 'database=master', 'uid=sa', 'server=mpilgrim']
>
> >>> ";".join(["%s=%s" % (k, v) for k, v in params.items()])
>
> 'pwd=secret;database=master;uid=sa;server=mpilgrim'
>
> >>>
>
> Thank you.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130301/0a9a81e9/attachment-0001.html>


More information about the Tutor mailing list