Dictionaries
Dictionaries are an Ordered Collection of Key-Value Pairs (Python 3.7 and Later)¶
Starting from Python 3.7, dictionaries maintain the insertion order of items. This means the order in which you add key-value pairs is preserved when you iterate over the dictionary.
Let's look at examples¶
# Creating a dictionary
person = {
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
# Adding a new key-value pair
person["email"] = "alice@example.com"
# Iterating over the dictionary
for key, value in person.items():
print(key, value)
Output:
In this example, the order of key-value pairs is the same as the order in which they were added to the dictionary.