Can python create a dictionary from a list comprehension?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Mon May 28 05:16:01 EDT 2007
En Mon, 28 May 2007 05:37:12 -0300, Wim Vogelaar
<wim.vogelaaratmc2worlddotorg at bag.python.org> escribió:
> I made the original list two elements longer: a =
> [1,2,3,4,5,6,7,8,9,10,11,12]
>
> and to my surprise the output is now ordered, giving: {2: 3, 4: 5, 6: 7,
> 8:
> 9, 10: 11, 12: 13}
>
> I am running ActiveState ActivePython 2.5
Keys in a dictionary are listed in an arbitrary order; the *only* thing
about the ordering you can say is that, given a FIXED dictionary (already
constructed, and without any other intervening operation that could alter
its content), when you iterate over its keys (using .keys(), .iterkeys()),
its values (.values(), .itervalues()) or items (.items(), .iteritems())
you will always get the same things in the same order over and over.
If you create the dictionary using a different sequence of insertions and
deletions, you may get different results. If you insert and delete things
afterwards, you may get different results. If you exit the program and run
it again, you may get different results. The *only* guaranteed fact is
that you will get the same results provided you don't modify the
dictionary at all.
See note (3) in http://docs.python.org/lib/typesmapping.html
--
Gabriel Genellina
More information about the Python-list
mailing list