[Tutor] Q about .join() Thanks!

Danny Yoo dyoo at hashcollision.org
Mon Feb 13 21:10:10 EST 2017


> That's because dictionaries are not stored sequentially and the
> order of retrieval is not guaranteed - it can even change
> during the execution of a program so you should never
> depend on it. That's because dictionaries are optimised
> for random access via the keys not to be iterated over.

Moreover, most implementations *deliberately* randomize their iteration
order to avoid a particular kind of hash collision attack out there in
the wild.  See:

    https://en.wikipedia.org/wiki/Collision_attack

    https://arstechnica.com/business/2011/12/huge-portions-of-web-vulnerable-to-hashing-denial-of-service-attack/

    https://mail.python.org/pipermail/python-dev/2011-December/115116.html

for some details.


> I've a vague memory that recent versions of Python may have
> a special dictionary type that does return items in the order
> they were inserted, but I may be mixing that up with
> another language... Hopefully someone else can provide
> a steer there.

In Python, it's collections.OrderedDict:

    https://docs.python.org/3.6/library/collections.html#collections.OrderedDict

Other languages have similar libraries.  Java has LinkedHashMap, for example:

    https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html


Best of wishes!


More information about the Tutor mailing list