How to add an element to a dictionary in Python

1 min

You very rarely define your final dictionary at the beginning of your script.

Why?

Well, because dictionaries are usually a way to store data that was computed within your script.

Here is how to add an element to a dictionary.

# We define a non complete student profile stored in a dict
student_profile = {
	"firstname" : "John",
    "lastname" : "Doe"
}

# We add up a key : value element
# Here it is a grade
student_profile["grade"] = 5.7
How to add an element to a dictionary

Here you are! You now know how to add an element to a dictionary in Python!

More on fundamentals

If you want to know more about Python fundamentals without headaches... check out the other articles I wrote by clicking just here:

Fundamentals - The Python You Need
We gathered the only Python essentials that you will probably ever need.