DarioM
0
Q:

python - deque example

from collections import OrderedDict 
od = OrderedDict()
1
An OrderedDict is a dictionary subclass that remembers the order 
that keys were first inserted. A regular dict doesn’t track the 
insertion order, and iterating it gives the values in an arbitrary 
order.
# A Python program to demonstrate working of OrderedDict 
from collections import OrderedDict 
d = {} # regular dict
d['a'] = 1
d['b'] = 2
d['c'] = 3
for key, value in d.items(): 
    print(key, value) 
# Output
# ('a', 1)
# ('c', 3)
# ('b', 2)  
od = OrderedDict() # Ordered Dict
od['a'] = 1
od['b'] = 2
od['c'] = 3
for key, value in od.items(): 
    print(key, value) 
# Output
# ('a', 1)
# ('b', 2)
# ('c', 3)
1
# It is usefull when dealing with treads
from collections import deque
students = deque(('Sarah', 'Mary', 'Carl'))
students.append('Charlotte')                   # this name appear at the end of the deque
students.appendleft('Ruth')                    # this name appear at the start of the deque
students.pop()                                 # remove the element from the end
students.popleft()                             # remove the element from the start 
print(students)    
1

New to Communities?

Join the community