Skip to content

Commit 96707d7

Browse files
Added
1 parent a71f219 commit 96707d7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

snippets/python/OrderedDict.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# OrderedDict in Python
2+
An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted.
3+
The only difference between dict() and OrderedDict() is that:
4+
5+
OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the
6+
insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the
7+
items are inserted is remembered by OrderedDict.
8+
9+
_tags_: OrderedDict
10+
11+
12+
# Snippet
13+
```
14+
# A Python program to demonstrate working of OrderedDict
15+
16+
from collections import OrderedDict
17+
18+
print("This is a Dict:\n")
19+
d = {}
20+
d['a'] = 1
21+
d['b'] = 2
22+
d['c'] = 3
23+
d['d'] = 4
24+
25+
for key, value in d.items():
26+
print(key, value)
27+
28+
print("\nThis is an Ordered Dict:\n")
29+
od = OrderedDict()
30+
od['a'] = 1
31+
od['b'] = 2
32+
od['c'] = 3
33+
od['d'] = 4
34+
35+
for key, value in od.items():
36+
print(key, value)
37+
```

0 commit comments

Comments
 (0)