Lie Ryan wrote:
Emile van Sebille wrote:
Mark Tolonen wrote:
[dict(n) for n in set(tuple(n.items()) for n in l1)]
Anyone know if the ordered items for two different dicts where dicta
== dictb is guaranteed the same? I know the ordering is unspecified
but you can depend on the sequence of keys matching data and I think
it'll also match items.
but for all DA's and DB's below, is that true?
Considering how dictionary is implemented, I don't think so. The
dictionary is internally implemented as a hash table, and two dictionary
with identical items may have different sized hash table, the algorithm
guarantees efficient usage of memory (it resizes when it's too small or
too big). On a resize, the hashes are recalculated and the ordering
rescrambled.
And a short demo to confirm:
Python 2.3.4 (#1, Jul 25 2008, 14:24:21)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = range(10)
>>> y = x[:]
>>> y.reverse()
>>> d1 = dict([(i*1000, i) for i in x])
>>> d2 = dict([(i*1000, i) for i in y])
>>> d1.items()
[(0, 0), (4000, 4), (1000, 1), (8000, 8), (6000, 6), (9000, 9), (2000, 2),
(5000, 5), (7000, 7), (3000, 3)]
>>> d2.items()
[(8000, 8), (4000, 4), (2000, 2), (0, 0), (9000, 9), (6000, 6), (5000, 5),
(3000, 3), (1000, 1), (7000, 7)]
Sincerely,
Albert
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor