What is a dictionary in Python?

A dictionary stores data as key: value pairs inside curly braces { }. You use the key to get the value. Keys must be unique, and from Python 3.7 a dictionary also remembers the order in which items were added.

student = {"name": "Robin", "age": 20}

print(student["name"])
student["city"] = "Delhi"
print(student)
print(student.get("phone", "not found"))

Output:

Tutorials Class - Output Window

Robin
{'name': 'Robin', 'age': 20, 'city': 'Delhi'}
not found

Using get() is safer than square brackets, because it does not raise a KeyError when the key is missing. See the Python Dictionary chapter for more methods.


Learn more about the similar topics:
Tutorials
Python Lists
Python Tuples
Python Sets
Python Dictionary
Exercises & Assignments
Python program to find largest and smallest number in a list
Python program to remove duplicates from a list
Interview Questions & Answers
What is a list in Python?
What is a tuple in Python?
What is a set in Python?
What is a dictionary in Python?
What is a negative index in Python and why is it used?
What is the difference between a list and a tuple in Python?
What are mutable and immutable types in Python?