For loop in dictionaries


#1

<
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?
link:
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/this-is-key?action=lesson_resume&link_content_target=interstitial_undefined


#2

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)


#3

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