Python - Append items at beginning of dictionary

Last Updated : 15 Jul, 2025

The task of appending items at the beginning of a dictionary in Python involves inserting new key-value pairs at the start of an existing dictionary, rather than adding them at the end. Since dictionaries in Python preserve insertion order, this operation allows us to effectively reorder the dictionary and ensuring that newly added items appear at the front.

For example, consider two dictionaries: a = {"Gfg": 5, "is": 3, "best": 10} and b = {"pre1": 4, "pre2": 8}. When we want to append the items from dictionary b to the beginning of a, it ensures that the elements from b come first, while preserving the original order of a. The output would be {'pre1': 4, 'pre2': 8, 'Gfg': 5, 'is': 3, 'best': 10} .

Using unpacking

** unpacking method is the most efficient way to append items at the beginning of a dictionary. This technique allows us to combine two dictionaries, where the items of one dictionary are placed at the beginning of the other.

Python
a = {"Gfg": 5, "is": 3, "best": 10}
b = {"pre1": 4, "pre2": 8}

b = {**b, **a}
print(str(b))

Output
{'pre1': 4, 'pre2': 8, 'Gfg': 5, 'is': 3, 'best': 10}

Explanation:

  • {**b, **a} merges two dictionaries into one .
  • **a unpacks all the key-value pairs from dictionary a and places them in the new dictionary.
  • **b does the same for dictionary b, adding its key-value pairs after those from a.

Using OrderedDict

OrderedDict from the collections module preserves the insertion order of keys. This method works well if we need to ensure that the order of insertion is preserved when appending items to the beginning of a dictionary.

Python
from collections import OrderedDict

a = {"Gfg": 5, "is": 3, "best": 10}
b = {"pre1": 4, "pre2": 8}

ordered_b = OrderedDict(b)
ordered_b.update(a)

print(ordered_b)

Output
OrderedDict({'pre1': 4, 'pre2': 8, 'Gfg': 5, 'is': 3, 'best': 10})

Explanation:

  • OrderedDict(b) creates an ordered_b initialized with b, preserving b's insertion order.
  • ordered_b.update(a) updates ordered_b with a and existing keys in ordered_b are updated with values from a .

Using update()

This method works by iterating over one dictionary and inserting its items at the beginning of another dictionary one by one. While it effectively appends the items, it’s not very efficient for larger dictionaries due to the repetitive creation of new dictionaries.

Python
a = {"Gfg": 5, "is": 3, "best": 10}
b = {"pre1": 4, "pre2": 8}

for i, j in a.items():
    b.update({i: j})
print(str(b))

Output
{'pre1': 4, 'pre2': 8, 'Gfg': 5, 'is': 3, 'best': 10}

Explanation:

  • for i, j in a.items() iterates over each key-value pair in a, with i as the key and j as the value.
  • b.update({i: j}) updates dictionary b by adding or modifying the key-value pair {i: j}.

Using dict()

dict() can also be used to append items at the beginning of a dictionary. It creates a new dictionary by first unpacking one dictionary and then adding the contents of another.

Python
a = {"Gfg": 5, "is": 3, "best": 10}
b = {"pre1": 4, "pre2": 8}

res = dict(b, **a)
print(res)

Output
{'pre1': 4, 'pre2': 8, 'Gfg': 5, 'is': 3, 'best': 10}

Explanation:

  • dict(b, **a) creates a new dictionary, starting with the key-value pairs from b and then adding the pairs from a.
  • If there were overlapping keys, the values from a would overwrite those from b.
Comment

Explore