Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
Is the key order the same for OrderedDict and dict?

dict keeps insertion order since Python 3.6 (see this).

OrderedDict was developed just for this purpose (before Python 3.6).

Since Python 3.6, is the key order always the same for dict or OrderedDict?

I wonder whether I can do this in my code and have always the same behavior (except of equality, and some extended methods in OrderedDict) but more efficiently:

if sys.version_info[:2] >= (3, 6):
  OrderedDict = dict
else:
  from collections import OrderedDict 

Or phrased differently, for Python >=3.6, is there any reason to use OrderedDict?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I realize that the different behavior of equality (__eq__) can be actually a major concern, why such code snippet is probably not good.

However, you could maybe still do this:

if sys.version_info[:2] >= (3, 6):
  OrderedDict = dict
else:
  from collections import OrderedDict as _OrderedDict

  class OrderedDict(_OrderedDict):
    __eq__ = dict.__eq__
    __ne__ = dict.__ne__

The difference is that with the original collections.OrderedDict, {1:1,2:2} is not the same as {2:2,1:1}, but for dict and my overwritten OrderedDict in this example, it is the same.

over 4 years ago · Santiago Trujillo Relatório

0

Some behaviour remains the same, but OrderedDict are reversible and dict are not:

from collections import OrderedDict

d = { "a" : 1, "c" : 2}

od = OrderedDict(d.items())

print(list(reversed(od)))
print(list(reversed(d)))

Output

['c', 'a']

Traceback (most recent call last):
  File "path/to/file", line 8, in <module>
    print(list(reversed(d)))
TypeError: 'dict' object is not reversible

From the documentation:

In addition to the usual mapping methods, ordered dictionaries also support reverse iteration using reversed().

over 4 years ago · Santiago Trujillo Relatório

0

Both OrderedDict and dict are insertion-ordered¹ for iteration. There is practically no reason to use OrderedDict if iteration order is the only deciding point, especially if re-ordering is not needed.
Obviously, if comparison order is desired OrderedDict and dict are not interchangeable.

Or phrased differently, for Python >=3.6, is there any reason to use OrderedDict?

These days OrderedDict is to dict what deque is to list, basically. OrderedDict/deque are based on linked lists² whereas dict/list are based on arrays. The former have better pop/move/FIFO semantics, since items can be removed from the start/middle without moving other items.

Since arrays are generally very cache friendly, the linked list advantage only comes into play for very large containers. Also, OrderedDict (unlike deque) does not have guarantees for its linked list semantics and its advantage may thus not be portable. OrderedDict should primarily be used if many pop/move/FIFO operations are needed and benchmarking can compare the performance of dict vs. OrderedDict in practice.


¹This applies to all currently supported implementations compliant with the Python language spec, i.e. CPython and PyPy since Python 3.6.

²OrderedDict in CPython still preserves O(1) key access. This is realised by also having a "regular" lookup table, using the linked list for order between items and the lookup table for direct item access. It's complicated.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda