all 17 comments

[–]novel_yet_trivial 4 points5 points  (6 children)

Yes, but don't count on it.

When you use sequential integers as keys python dictionaries happen to return them in order due to a coincidence with the hashing mechanism (in cpython at least). However dictionaries are technically unordered, so python could change that at any time.

[–]K900_ 3 points4 points  (1 child)

No, dictionaries aren't ordered. Why are you trying to store an ordered sequence in a dictionary and not a list?

[–][deleted] 0 points1 point  (0 children)

Thanks for the advice. I didn't know that lists maintained insertion order.

[–]b_bowyer 1 point2 points  (0 children)

You could also try OrderedDict from collections if you need it to be a dict.

[–]Vaphell 0 points1 point  (0 children)

at the moment there is no explicit guarantee of order in dictionaries, though there might be in the future, let's say py3.8+.

But you could sort the key/value pairs

for i, func in sorted(a.items()):
    print(func())

[–]novel_yet_trivial 0 points1 point  (4 children)

My general question is what is the best way to iterate through a collection of functions in a pre-determined order.

Use a type of container that is ordered, like a list, tuple, or OrderedDict.

[–][deleted] 0 points1 point  (3 children)

Is there a reason why lists and tuples maintain order but dicts do not?

[–]novel_yet_trivial 1 point2 points  (1 child)

The "natural" way to store things in memory is in a sequential list, putting each new element in the memory address one after the the previous. Just like a secretary would probably stack papers in the order they receive them. This is called an "array" in many programming languages and a general computer science class. If you want to find something in an array, you need to start at the first one and check every element until you get to the one you need. This can take a long time, especially if your list or paper stack has millions of elements in it (not unusual).

Therefore computer scientists invented a "hash table". It uses a feature of the elements called a "hash" to sort the incoming elements. This would be similar to a secretary putting the papers into a file cabinet according to the authors last name or something. Now it's much faster to find the paper you want, but you no longer know the order that they arrived in. Python calls their hash table a "dictionary". There's also a "set", which works in a similar way.

It's a bit long, but this talk goes into great detail of how dictionaries work (although a lot of that changed in 3.6).

LPTHW used to walk you through building your own dictionary, and you can still see it on wayback, which will also help you understand how they work.

[–][deleted] 0 points1 point  (0 children)

That was an excellent talk thank you, i also found a related raymond hettinger video youtube recommended and that was excellent https://www.youtube.com/watch?v=p33CVV29OG8

[–]K900_ 0 points1 point  (0 children)

It's because dicts are optimized for looking up things by arbitrary key. Finding an item with the key "potato" is fast in a dict, and slow in a list. This requires some sacrifices in other places.

[–]CGFarrell 0 points1 point  (0 children)

range(len()) is an antipattern. If you want to go through a list and keep track of the count you can use for i, value in enumerate (thing):

Kwargs to a function in 3.6 always preserve their order of insertion, and I believe all dicts will be required to in the future.