Member-only story
Python’s dictionaries are incredibly versatile data structures that allow you to store and manage data in a way that is both efficient and intuitive. In this article, we’ll explore the ins and outs of dictionaries, covering everything from creating and accessing them to modifying their contents and leveraging their built-in methods.
Creating Dictionaries
Dictionaries in Python are defined using curly braces {} and consist of key-value pairs separated by colons. Here’s how you can create a new dictionary:
person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
In this example, we’ve created a dictionary called person
with three key-value pairs: 'name'
is associated with the value 'Alice'
, 'age'
is associated with the value 30
, and 'city'
is associated with the value 'New York'
.
Accessing Dictionary Values
To access the value associated with a particular key in a dictionary, you can use square bracket notation:
print(person['name']) # Output: Alice
print(person['age']) # Output: 30