To access values in a dictionary in Python, you use the key associated with the value you want to retrieve. Here’s how to do it:
Syntax:
value = dictionary[key]
Example:
contacts = {
"Alice": {"phone": "123-456-7890", "email": "alice@example.com"},
"Bob": {"phone": "987-654-3210", "email": "bob@example.com"}
}
# Accessing Alice's phone number
alice_phone = contacts["Alice"]["phone"]
print(alice_phone) # Output: 123-456-7890
# Accessing Bob's email
bob_email = contacts["Bob"]["email"]
print(bob_email) # Output: bob@example.com
Summary:
- Use the key in square brackets to access the corresponding value.
- If the value is another dictionary, you can chain the keys to access nested values.
If you have more questions or need further clarification, feel free to ask!
