From 96707d7a9e0225a538dbdd7473ed6d8ada07f47d Mon Sep 17 00:00:00 2001 From: Kamesh Mishra <58304583+Kamesh-Mishra@users.noreply.github.com> Date: Tue, 20 Oct 2020 20:23:48 +0530 Subject: [PATCH 1/2] Added --- snippets/python/OrderedDict.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 snippets/python/OrderedDict.md diff --git a/snippets/python/OrderedDict.md b/snippets/python/OrderedDict.md new file mode 100644 index 0000000..4fbb4d2 --- /dev/null +++ b/snippets/python/OrderedDict.md @@ -0,0 +1,37 @@ +# OrderedDict in Python +An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. +The only difference between dict() and OrderedDict() is that: + +OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the +insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the +items are inserted is remembered by OrderedDict. + +_tags_: OrderedDict + + +# Snippet +``` +# A Python program to demonstrate working of OrderedDict + +from collections import OrderedDict + +print("This is a Dict:\n") +d = {} +d['a'] = 1 +d['b'] = 2 +d['c'] = 3 +d['d'] = 4 + +for key, value in d.items(): + print(key, value) + +print("\nThis is an Ordered Dict:\n") +od = OrderedDict() +od['a'] = 1 +od['b'] = 2 +od['c'] = 3 +od['d'] = 4 + +for key, value in od.items(): + print(key, value) +``` \ No newline at end of file From 85a36b7d9fc471afe3d15ef577a8cc974c4e9bca Mon Sep 17 00:00:00 2001 From: Kamesh Mishra <58304583+Kamesh-Mishra@users.noreply.github.com> Date: Tue, 20 Oct 2020 20:28:58 +0530 Subject: [PATCH 2/2] Added OrderedDict.md --- snippets/python/OrderedDict.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/python/OrderedDict.md b/snippets/python/OrderedDict.md index 4fbb4d2..d5f349a 100644 --- a/snippets/python/OrderedDict.md +++ b/snippets/python/OrderedDict.md @@ -8,7 +8,6 @@ items are inserted is remembered by OrderedDict. _tags_: OrderedDict - # Snippet ``` # A Python program to demonstrate working of OrderedDict @@ -34,4 +33,4 @@ od['d'] = 4 for key, value in od.items(): print(key, value) -``` \ No newline at end of file +```