Skip to content
Advertisement

how to do a dictionary that give the output with a fifo process

i am searching a method or library that use a dict that allow method like: element_dict_in({'polo':789}),and element_dict_out() that return me the first relation that was put in the dictionary, the 2 method that i mentioned before are not implemented,it is for clarify my idea:

for example:

dict={}
element_dict_in({'polo1':789})
element_dict_in({'polo2':123})
element_dict_in({'polo3':4556})#{'polo1':789,'polo2':123,'polo3':4556}
element_dict_out()#return {'polo1':789}

i find this link pythonic way for FIFO order in Dictionary but for my it is not enough clear, so exist something like that?

Advertisement

Answer

Python actually already has this in the standard library – collections.OrderedDict.

from collections import OrderedDict

my_dict = OrderedDict()
my_dict['polo1'] = 789
my_dict['polo2'] = 123
my_dict['polo3'] = 4556

print(my_dict.popitem(last=False))
# ('polo1', 789)

Notably, the built-in dict type can do LIFO popping but not FIFO popping if that’s acceptable to you, and is generally faster than the OrderedDict for most things.

Advertisement