CYBER WEEK - EXTRA SAVINGS EVENT

# Wrong order when assigning tuple(np.array) to a dictionary as keys

Hello everyone,

I have a 2 dimensional np array and I want to take each row, transform it to a tuple and assign it to a dictionary in the same order with their values as empty np arrays. However as I do that, the order of the entries changes. Can you please help me understand why?

The order of the rows in my 2d np array below is:
1- [2, 4]
2- [3, 6]
3- [5, 3]
4- [3, 1]

However my code below creates a dictionary with the order:
1- [3, 1]
2- [2, 4]
3- [3, 6]
4- [5, 3]

My Code:

``````points = np.array([[2, 4], [3, 6], [5, 3], [3, 1]])

points_dic = {tuple(point) : np.array([]) for point in points}

for key in points_dic:
print(key)

``````

What I expected to happen:
Output:

``````(2, 4)
(3, 6)
(5, 3)
(3, 1)
``````

What actually happened:
Output:

``````(3, 1)
(2, 4)
(3, 6)
(5, 3)
``````

Hi @yigitsoykaan. What version of python are you running? As of 3.7, python dictionaries preserve order. If your version is >=2.7 and <3.6 then a couple options are to either upgrade to 3.7+ or you could use an ordered dictionary as this code below demonstrates:

``````import collections

points = np.array([[2, 4], [3, 6], [5, 3], [3, 1]])

points_dic = collections.OrderedDict([((2,4), []), ((3,6), []), ((5,3), []), ((3,1), [])])

for key in points_dic:
print(key)

Output:
(2, 4)
(3, 6)
(5, 3)
(3, 1)
``````

You may want to check out this StackOverflow post for more details.

To see what version of python youâ€™re using, try this:

``````import sys

print(sys.version)
``````

Note: I tried this code on the DQ platform and discovered it is using Python 3.4.3 so it makes sense why youâ€™re seeing this unordered behaviour if thatâ€™s where you are writing your code.

1 Like

Hi Mike thank you for the clarification yes Iâ€™ve been running this code on DQ platform. So, in this case do you think I should submit a ticket for the version to be updated?

As you can imagine, this issue of â€śthe most recent version hasnâ€™t been installedâ€ť has come up in other areas: NumPy, pandas, matplotlibâ€¦and since each of these libraries has dependencies, once you start updating, you canâ€™t stop!

That then leads to: should DQ also update their content to reflect the newer functions/methods that are included in the updates? (Like dictionaries being ordered in 3.7+, for example.) I think not. At the end of the day, a line has to be drawn somewhere or the content team will forever be chasing their own tail. When you start trying to keep everything up to date, things invariably break and I personally really like the fact that the DQ platform is stable and reliable.

Also, using older versions makes you appreciate all the â€śshortcutsâ€ť that come with updates. I like having to do things â€śthe long wayâ€ť sometimesâ€¦it gives me a deeper/richer understanding of the python language. If I want to use newer functions/methods that arenâ€™t available on the DQ platform, I just run it locally.

So all that said, I donâ€™t think itâ€™s worth your time to submit a ticket.

1 Like