English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

OrderedDict in Python

OrderedDict is a subclass of the dict object in Python. The only difference between OrderedDict and dict is that in OrderedDict, it maintains the order of the inserted keys. In the dictionary, sorting may or may not occur.

OrderedDict is a standard library class located incollectionsModule.

Firstly, we need to import it to use it.SetStandard library module.

import collections

In this section, we will see some operations about OrderedDict and the differences between OrderedDict and Dict.

We can put some keys and values in Dict and OrderedDict. In this example, we can see that the order of Dict may be different. But for OrderedDict, it will be fixed.

Codice di esempio

import collections
#Create normal dict
my_dict = {}
my_dict['AA'] = 11
my_dict['BB'] = 22
my_dict['CC'] = 33
my_dict['DD'] = 44
for item in my_dict.items():
   print(item)
print()
#Creazione di dict ordinato
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)

Risultato di output

('AA', 11)
('CC', 33)
('BB', 22)
('DD', 44)
('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)

Change the value of the specified key

When the value of the specified key changes, for OrderedDict, the order of the keys will not change. We can see that this statement may be correct or incorrect for Dict type objects.

Codice di esempio

import collections
#Create normal dict
my_dict = {}
my_dict['AA'] = 11
my_dict['BB'] = 22
my_dict['CC'] = 33
my_dict['DD'] = 44
for item in my_dict.items():
   print(item)
#Cambiamento del valore per la chiave BB
my_dict['BB'] = 100
print('Dopo cambiamento nel Dict')
for item in my_dict.items():
   print(item)
print()
#Creazione di dict ordinato
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)
#Cambiamento del valore per la chiave BB
my_ord_dict['BB'] = 100
print('Dopo cambiamento nell'OrderedDict')
for item in my_ord_dict.items():
   print(item)

Risultato di output

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
Dopo cambiamento nel Dict
('AA', 11)
('CC', 33)
('DD', 44)
('BB', 100)
('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
Dopo cambiamento nell'OrderedDict
('AA', 11)
('BB', 100)
('CC', 33)
('DD', 44)

Eliminazione e ri-inserimento di elementi nell'OrderedDict

Quando si elimina un elemento dall'OrderedDict e si esegue la re-inserzione della chiave e del valore, esso viene riportato all'indietro. Anche se mantiene l'ordine durante l'inserimento, per l'eliminazione, elimina le informazioni di ordinamento e viene riinserito come nuovo elemento.

Codice di esempio

import collections
#Creazione di dict ordinato
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)
#Cancellazione item in chiave BB
my_ord_dict.pop('BB')
print('Dopo cancellazione')
for item in my_ord_dict.items():
   print(item)
#ri-inserimento item 
my_ord_dict['BB'] = 22
print('Dopo ri-inserimento')
for item in my_ord_dict.items():
   print(item)

Risultato di output

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
Dopo cancellazione
('AA', 11)
('CC', 33)
('DD', 44)
Dopo ri-inserimento
('AA', 11)
('CC', 33)
('DD', 44)
('BB', 22)