Insertion at the beginning in OrderedDict using Python

When working with OrderedDict in Python, you may need to insert elements at the beginning rather than the end. Python's OrderedDict doesn't have a direct method for insertion at the beginning, but you can achieve this using the update() method combined with move_to_end().

What is OrderedDict?

OrderedDict is a dictionary subclass from the collections module that remembers the order in which items were inserted. This makes it useful when the sequence of key-value pairs matters.

Method: Using update() and move_to_end()

To insert at the beginning, first add the new item using update(), then move it to the front using move_to_end() with last=False ?

from collections import OrderedDict

# Create an OrderedDict with initial data
my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
print("Original dictionary:")
print(my_ordered_dict)

# Add new item and move to beginning
my_ordered_dict.update({'Mark': '7'})
my_ordered_dict.move_to_end('Mark', last=False)

print("Dictionary after inserting 'Mark' at the beginning:")
print(my_ordered_dict)
Original dictionary:
OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
Dictionary after inserting 'Mark' at the beginning:
OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])

How It Works

The process involves two steps:

  • update({'Mark': '7'}) - Adds the new key-value pair to the end of the OrderedDict
  • move_to_end('Mark', last=False) - Moves the specified key to the beginning (when last=False)

Inserting Multiple Items at the Beginning

You can insert multiple items at the beginning by repeating this process ?

from collections import OrderedDict

# Create an OrderedDict
data = OrderedDict([('A', '1'), ('B', '2')])
print("Original:", data)

# Insert multiple items at the beginning
new_items = [('X', '10'), ('Y', '20')]

for key, value in new_items:
    data.update({key: value})
    data.move_to_end(key, last=False)

print("After inserting X and Y at beginning:", data)
Original: OrderedDict([('A', '1'), ('B', '2')])
After inserting X and Y at beginning: OrderedDict([('Y', '20'), ('X', '10'), ('A', '1'), ('B', '2')])

Alternative: Creating a New OrderedDict

Another approach is to create a new OrderedDict with the new item first ?

from collections import OrderedDict

# Original dictionary
original = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
print("Original:", original)

# Create new OrderedDict with new item at the beginning
new_dict = OrderedDict([('Mark', '7')])
new_dict.update(original)

print("New dictionary with 'Mark' at beginning:", new_dict)
Original: OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')])
New dictionary with 'Mark' at beginning: OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])

Conclusion

Use update() followed by move_to_end(key, last=False) to insert items at the beginning of an OrderedDict. For multiple insertions, consider creating a new OrderedDict for better performance.

Updated on: 2026-03-25T19:15:04+05:30

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements