Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python OrderedDict

# 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
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #OrderedDict
ADD COMMENT
Topic
Name
2+6 =