× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Python dicts do not have the concept of order!

Aur Saraf
Sep 13, 2014
<pre><code>&gt;&gt;&gt; d = {} &gt;&gt;&gt; d[1] = 0 &gt;&gt;&gt; d[2] = 0 &gt;&gt;&gt; d {2: 0, 1: 0} &gt;&gt;&gt; d {1: 0, 2: 0} &gt;&gt;&gt; l = [] &gt;&gt;&gt; l.append((1, 0)) &gt;&gt;&gt; l.append((2, 0)) &gt;&gt;&gt; l [(1, 0), (2, 0)] &gt;&gt;&gt; l [(1, 0), (2, 0)]</code></pre><p>You can add items in one order and they will be printed in a different order. Nobody even promises you that if printed twice they will be printed in the same order both times!</p><p> </p><p>If you want to keep order, just use a list of pairs like (1, 2). Lists always stay in the same order.</p>
comments powered by Disqus