Are dictionaries ordered in Python 3.6+?

Introduction

In Python, dictionaries are a fundamental data structure that allows us to store key-value pairs. Previously, in Python versions before 3.6, dictionaries were unordered. However, starting from Python 3.6, dictionaries are ordered.

In this article, we will explore the new dictionary implementation introduced in Python 3.6 and discuss how it performs better while preserving element order. We will also look at the reasons behind this change and explore some examples to understand the concept better.

New Dictionary Implementation

Python 3.6 introduced a new dictionary implementation that uses a "compact" representation pioneered by PyPy. This new implementation offers several advantages over the older one, including reduced memory usage.

The order-preserving aspect of this new implementation is considered an implementation detail and is not a language feature. Therefore, it should not be relied upon and may change in future versions of Python. However, it has been desired to have this new dict implementation for a few releases to ensure backwards compatibility with older versions of the language.

It is important to note that the order-preserving behavior is not guaranteed in Python versions before 3.6. Python 3.5 and earlier versions still have random iteration order.

Performance Improvement

The new dictionary implementation in Python 3.6 offers performance improvements over the older implementation while preserving element order. The main advantage is the reduced memory usage, which is between 20% and 25% smaller compared to Python 3.5.

This improvement in memory usage can be attributed to the "compact" representation of the new dictionary implementation. By optimizing the data structure, Python is able to achieve better memory utilization without compromising on functionality or performance.

The reduced memory usage also has a positive impact on performance. With less memory being used, the overall performance of Python programs that heavily utilize dictionaries improves.

Examples

Lets consider a simple example to understand how dictionaries are ordered in Python 3.6+:


            # Example 1
            my_dict = {'a': 1, 'b': 2, 'c': 3}
            print(my_dict)
        

The above code will output:


            {'a': 1, 'b': 2, 'c': 3}
        

As we can see, the dictionary elements are printed in the same order as they were inserted, indicating that the new dictionary implementation in Python 3.6 preserves element order.

Now, let's consider another example where we insert the elements in a different order:


            # Example 2
            my_dict = {'b': 2, 'c': 3, 'a': 1}
            print(my_dict)
        

The output of the above code will be:


            {'b': 2, 'c': 3, 'a': 1}
        

Again, the dictionary elements are printed in the same order as they were inserted, demonstrating that the order is preserved regardless of the order of insertion.

Conclusion

In conclusion, starting from Python 3.6, dictionaries are ordered. The new dictionary implementation in Python 3.6 offers improved performance and reduced memory usage while preserving element order. However, it is important to note that the order-preserving behavior is considered an implementation detail and is not guaranteed in Python versions before 3.6.

It is always recommended to write code that does not rely on the order of elements in dictionaries, as this behavior may change in future versions of Python. By understanding the new dictionary implementation and its advantages, Python developers can make informed decisions when working with dictionaries in their programs.