For loop in dictionaries


why does for loop when applied to a list goes though evry key but not in a particular order like when for loop is applied to a list?


dict doesn’t represent order, there’s no particular order to use when looping.

In simplified terms, dicts compute the location of the value from the key, meaning that the order in memory (may or may not) be affected by insertion order or other operations.

So what it comes down to is that you should know what your data structure promises, and only rely on those things, regardless of any undefined behaviour that it might also have (like iteration order)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.