Python Practice › Dictionary Programs › Program 21
Insert a key at the beginning
Write a Python program to insert a key at the beginning of a dictionary.
Input
{"course": "python", "institute": "sqatools"}, insert ("name", "omkar")Output
{'name': 'omkar', 'course': 'python', 'institute': 'sqatools'}Approach
- Dicts keep insertion order, so build a new dict with the new key first.
Solution
d = {"course": "python", "institute": "sqatools"}d = {"name": "omkar", **d}print(d)Output
{'name': 'omkar', 'course': 'python', 'institute': 'sqatools'}Learn the concept
New to this topic? Read the lesson, then come back.