2015-03-17 19:25:04 -04:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
|
|
|
except ImportError:
|
2018-05-14 14:43:34 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2015-03-17 19:25:04 -04:00
|
|
|
|
|
|
|
d = OrderedDict([(10, 20), ("b", 100), (1, 2)])
|
2017-03-02 19:23:54 -05:00
|
|
|
print(len(d))
|
2015-03-17 19:25:04 -04:00
|
|
|
print(list(d.keys()))
|
|
|
|
print(list(d.values()))
|
|
|
|
del d["b"]
|
2017-03-02 19:23:54 -05:00
|
|
|
print(len(d))
|
|
|
|
print(list(d.keys()))
|
|
|
|
print(list(d.values()))
|
|
|
|
|
|
|
|
# access remaining elements after deleting
|
|
|
|
print(d[10], d[1])
|
|
|
|
|
|
|
|
# add an element after deleting
|
|
|
|
d["abc"] = 123
|
|
|
|
print(len(d))
|
2015-03-17 19:25:04 -04:00
|
|
|
print(list(d.keys()))
|
|
|
|
print(list(d.values()))
|
2020-04-22 11:10:30 -04:00
|
|
|
|
|
|
|
# pop an element
|
|
|
|
print(d.popitem())
|
|
|
|
print(len(d))
|
|
|
|
print(list(d.keys()))
|
|
|
|
print(list(d.values()))
|
|
|
|
|
|
|
|
# add an element after popping
|
|
|
|
d["xyz"] = 321
|
|
|
|
print(len(d))
|
|
|
|
print(list(d.keys()))
|
|
|
|
print(list(d.values()))
|
|
|
|
|
|
|
|
# pop until empty
|
|
|
|
print(d.popitem())
|
|
|
|
print(d.popitem())
|
|
|
|
try:
|
|
|
|
d.popitem()
|
|
|
|
except:
|
|
|
|
print('empty')
|
2023-07-15 00:53:58 -04:00
|
|
|
|
|
|
|
# fromkeys returns the correct type and order
|
|
|
|
d = dict.fromkeys('abcdefghij')
|
|
|
|
print(type(d) == dict)
|
|
|
|
d = OrderedDict.fromkeys('abcdefghij')
|
|
|
|
print(type(d) == OrderedDict)
|
|
|
|
print(''.join(d))
|
|
|
|
|
|
|
|
# fromkey handles ordering with duplicates
|
|
|
|
d = OrderedDict.fromkeys('abcdefghijjihgfedcba')
|
|
|
|
print(''.join(d))
|