File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments