How to reorder dynamic string keys for dictionary to save it to JSON

How to reorder dynamic string keys for dictionary to save it to JSON

How to sort the dynamic updated dictionary data every time new keys are updated in the dictionary? It gives me a random order for the keys whenever i try to print the dictionary after updating.

How to sort the dynamic updated dictionary data every time new keys are updated in the dictionary? It gives me a random order for the keys whenever i try to print the dictionary after updating.

Using Sorted function doesn't seem to work in sorting the data dynamically. I keep getting random order whenever new keys are added to dictionary and i need to sort them and update them in JSON formatted data.

mydict ={"item_0": "apples"}
print(mydict)
mydict.update(item_1="banana")
print(mydict)
mydict.update(item_2="oranges")
print(mydict)
mydict.update(item_3="peaches")
print(mydict)
mydict.update(item_4="tangerines")
print(mydict)

Actual Results {'item_0': 'apples'} {'item_0': 'apples', 'item_1': 'banana'} {'item_2': 'oranges', 'item_0': 'apples', 'item_1': 'banana'} {'item_2': 'oranges', 'item_3': 'peaches', 'item_0': 'apples', 'item_1': 'banana'} {'item_4': 'tangerines', 'item_2': 'oranges', 'item_3': 'peaches', 'item_0': 'apples', 'item_1': 'banana'}

Expected Results {'item_0': 'apples'} {'item_0': 'apples', 'item_1': 'banana'} {'item_0': 'apples', 'item_1': 'banana', 'item_2': 'oranges'} {'item_0': 'apples', 'item_1': 'banana', 'item_2': 'oranges', 'item_3': 'peaches'} {'item_0': 'apples', 'item_1': 'banana', 'item_2': 'oranges', 'item_3': 'peaches', 'item_4': 'tangerines'}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

Python Tutorial: Working with JSON Data using the json Module

In this Python Programming Tutorial, we will be learning how to work with JSON data. We will learn how to load JSON into Python objects from strings and how to convert Python objects into JSON strings. We will also see how to load JSON from a file and save those Python objects back to files