
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What are Ordered dictionaries in Python?
An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, supporting the usual dict methods.If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
>>> from collections import OrderedDict >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'mango': 2} >>> od=OrderedDict(d.items()) >>> od OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('mango', 2)]) >>> od=OrderedDict(sorted(d.items())) >>> od OrderedDict([('apple', 4), ('banana', 3), ('mango', 2), ('pear', 1)]) >>> t=od.popitem() >>> t ('pear', 1) >>> od=OrderedDict(d.items()) >>> t=od.popitem() >>> t ('mango', 2)
- Related Articles
- How to create Ordered dictionaries in Python?
- Python – Filter dictionaries with ordered values
- How are dictionaries implemented in Python?
- How expensive are Python dictionaries to handle?
- Handling missing keys in Python dictionaries
- get() method for dictionaries in Python
- Python – Ordered tuples extraction
- Python – Sort Dictionaries by Size
- Python - Difference in keys of two dictionaries
- Flatten given list of dictionaries in Python
- How do we compare two dictionaries in Python?
- How to perform Calculations with Dictionaries in Python?
- Python Program to compare elements in two dictionaries
- How to merge multiple Python dictionaries?
- Python program to merge to dictionaries.

Advertisements