# How to insert dict key after specific key

**URL:** <https://discuss.python.org/t/how-to-insert-dict-key-after-specific-key/4198>\
**Category:** Python Help\
**Created:** [May 15, 2020, 2:05pm UTC](https://discuss.python.org/t/how-to-insert-dict-key-after-specific-key/4198 "2020-05-15T14:05:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![imrankhan\_pythonLang](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@imrankhan\_pythonLang](https://discuss.python.org/u/imrankhan_pythonLang)\
**Post date:** [May 15, 2020, 2:05pm UTC](https://discuss.python.org/t/how-to-insert-dict-key-after-specific-key/4198/1 "2020-05-15T14:05:00Z")

</div>

How can I insert key with value after specific key in a dictionary

Example:

> {“key1”: 5, “key2”: 10, “key3”: 15, “key4”: 20}

I want to insert after “key2”

> “key5”: 25

Final result something like this

> {“key1”: 5, “key2”: 10, “key5”: 25, “key3”: 15, “key4”: 20}

---

<div class="post-metadata">

**Author:** ![Rhodri](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@Rhodri](https://discuss.python.org/u/Rhodri)\
**Post date:** [May 15, 2020, 5:50pm UTC](https://discuss.python.org/t/how-to-insert-dict-key-after-specific-key/4198/2 "2020-05-15T17:50:20Z")

</div>

You can’t. Dictionaries aren’t really ordered; technically they preserve the order that items are added to the dictionary, but that’s something you’re better off forgetting about except when it comes to writing doctests. If you want to access the keys in a specific sequence, you are going to have to make a separate list of the keys and sort or keep it in the order you want yourself.

Why do you think you need to do this?
