
- 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
Insertion at the beginning in OrderedDict using Python
When it is required to insert the elements at the beginning of an ordered dictionary, the ‘update’ method can be used.
Below is the demonstration of the same −
Example
from collections import OrderedDict my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) print("The dictionary is :") print(my_ordered_dict) my_ordered_dict.update({'Mark':'7'}) my_ordered_dict.move_to_end('Mark', last = False) print("The resultant dictionary is : ") print(my_ordered_dict)
Output
The dictionary is : OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) The resultant dictionary is : OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])
Explanation
The required packages are imported.
An ordered dictionary is created using OrderedDict’.
It is displayed on the console.
The ‘update’ method is used to specify the key and the value.
The ‘move_to_end’ method is used to move a key value pair to the end.
The output is displayed on the console.
- Related Articles
- OrderedDict in Python
- Check order of character in string using OrderedDict( ) in Python
- How to match at the beginning of string in python using Regular Expression?
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Insert the string at the beginning of all items in a list in Python
- Insertion Sort in Python Program
- Program to sort all vowels at beginning then the consonants, are in sorted order in Python
- What is Insertion sort in Python?
- Python program to insert a new node at the beginning of the Doubly Linked list
- Python program to insert a new node at the beginning of the Circular Linked List
- Python Program for Insertion Sort
- Insertion sort using C++ STL
- Golang Program to add an element in the array at the beginning
- C++ Program to add an element in the array at the beginning
- Explain the insertion sort by using C language.

Advertisements