Quick actions

cmd+k|ctrl+k

Navigation

Languages

OrderedDict

Snippet info

Language

Python

Visibility

public

Author

hzx33737

Created

2023-05-16T03:38:10.245318Z

Updated

2023-05-16T03:49:10.931488Z

from collections import OrderedDict

#### USING NORMAL DICTIONARY ####
d1 = {} # creating dictionary
d1['a'] = 'Apple'
d1['b'] = 'Ball'

d2 = {} # creating dictionary
d2['b'] = 'Ball'
d2['a'] = 'Apple'

print(d1 == d2) # True
# As dictionary stores key values not in specific(insertion) order,
# hence if all keys and values are same then both are same


#### USING ORDERED DICTIONARY ####
od1 = OrderedDict() # creating ordered dictionary
od1['a'] = 'Apple'
od1['b'] = 'Ball'

od2 = OrderedDict() # creating ordered dictionary
d2['b'] = 'Ball'
d2['a'] = 'Apple'

print(od1 == od2) # False
# As ordered dictionary stores key values in specific(insertion) order,
# hence although all keys and values are same the dictionaries are not same
INFO