# Dictonary which stores order of data insertion like a queue
from collections import OrderedDict
#initialization
od = OrderedDict()
# or
od = OrderedDict({'a':1})
#Storing data from small to big values (not keys)
# Sorted with value, then key
#Adding Data
od['a'] = 1
od['d'] = 3
od['e'] = 7
od['f'] = 1
od['c'] = 7
od['b'] = 5
#Get all keys
od.keys()
#Get all key & value as list
od.items()
# Move an item to end
od.move_to_end('a', last=True) #Default last value = True
# Move an item to Start
od.move_to_end('a', last=False)
#Removing Data with key
del od['c']
od.pop('c') #Also returns the value
#Removing Top Element
od.popitem(last = False) #also return key/value pair
#Removing Bottom Element
od.popitem(last = True) #also return key/value pair
# Dictonary which stores order of data insertion like a queue
from collections import OrderedDict
#initialization
od = OrderedDict()
# or
od = OrderedDict({'a':1})
#Storing data from small to big values (not keys)
# Sorted with value, then key
#Adding Data
od['a'] = 1
od['d'] = 3
od['e'] = 7
od['f'] = 1
od['c'] = 7
od['b'] = 5
#Get all keys
od.keys()
#Get all key & value as list
od.items()
# Move an item to end
od.move_to_end('a', last=True) #Default last value = True
# Move an item to Start
od.move_to_end('a', last=False)
#Removing Data with key
del od['c']
od.pop('c') #Also returns the value
#Removing Top Element
od.popitem(last = False) #also return key/value pair
#Removing Bottom Element
od.popitem(last = True) #also return key/value pair
# A Python program to demonstrate working of OrderedDict
from collections import OrderedDict
print("This is a Dict:
")
d = {}
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
for key, value in d.items():
print(key, value)
print("
This is an Ordered Dict:
")
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
od['d'] = 4
for key, value in od.items():
print(key, value)
import collections
d1=collections.OrderedDict()
d1['A']=10
d1['C']=12
d1['B']=11
d1['D']=13
for k,v in d1.items():
print (k,v)
#Output:
A 10
C 12
B 11
D 13