Are dictionaries ordered in Python 3.6+?

Cover Image for Are dictionaries ordered in Python 3.6+?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Are dictionaries ordered in Python 3.6+? 📚

If you've ever worked with dictionaries in Python, you might have wondered if they maintain the order in which the key-value pairs are inserted. Well, the answer to that question is, yes, starting from Python 3.6!

The New Dictionary Implementation 🚀

In Python 3.6, a new implementation for dictionaries was introduced, using a "compact" representation pioneered by PyPy. This new implementation brings significant improvements in terms of memory usage, with the new dict() being between 20% and 25% smaller compared to Python 3.5.

The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon. In other words, while dictionaries are currently ordered in Python 3.6+, there's no guarantee that this will always be the case in future versions of the language.

This design decision was made to maintain backward compatibility with older versions of Python, where dictionaries had random iteration order. By allowing time for the new dict() implementation to be used in several releases, potential issues can be identified and addressed before making ordering a mandatory language feature.

The Future of Ordered Dictionaries 💡

If you're hoping for a firm guarantee of ordered dictionaries, I have exciting news for you! Starting from Python 3.7, ordered dictionaries are officially guaranteed. This means you can rely on the insertion order being preserved in future versions of Python.

Summary 📝

To summarize, dictionaries in Python 3.6+ are ordered, but this behavior is currently considered an implementation detail and may change in future versions. However, from Python 3.7 onwards, ordered dictionaries are guaranteed.

Now that you know the ins and outs of dictionary ordering in Python, you can confidently write code that relies on this behavior when working with key-value pairs.

So go ahead, embrace the power of ordered dictionaries and build amazing Python applications! 😎💻

Did you find this article helpful? Share your thoughts in the comments below and let's discuss the fascinating world of Python dictionaries! 🔥🐍

<hr />

Update December 2017: Dictionaries retaining insertion order is guaranteed for Python 3.7


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello