⬅ Previous Topic
Python ListsNext Topic ⮕
Python Tuples⬅ Previous Topic
Python ListsNext Topic ⮕
Python TuplesIn Python, a dictionary is a collection that allows you to store data in pairs. Each pair consists of a key and a value. Think of it as a real-world dictionary where you look up a word (key) and get its meaning (value).
A Python dictionary is an unordered collection that stores data as key-value pairs. Each key is unique, and each key maps to a value. You can use the key to access the associated value.
my_dict = {"name": "John", "age": 25, "city": "New York"}
In this dictionary, the keys are "name"
, "age"
, and "city"
. The corresponding values are "John"
, 25
, and "New York"
.
Dictionaries are very useful when you need to store data that is associated with a unique identifier (the key). For example, storing student names with their grades, a person's contact information (name, phone number, etc.), or the countries and their capitals.
To create a dictionary, you can use curly braces {}
and separate keys and values with a colon :
.
student = {"name": "Alice", "age": 22, "grade": "A"}
To get the value associated with a key, you can use square brackets []
with the key.
print(student["name"]) # Output: Alice
The above code prints the value associated with the key "name"
, which is Alice
.
If you want to change the value associated with a key, you can directly assign a new value to that key.
student["age"] = 23 # Now the age is updated
Now, if you print the dictionary, the age will be updated:
print(student) # Output: {"name": "Alice", "age": 23, "grade": "A"}
You can add a new key-value pair to the dictionary by assigning a value to a new key.
student["city"] = "London" # Adding a new key-value pair
After adding, the dictionary will look like this:
print(student) # Output: {"name": "Alice", "age": 23, "grade": "A", "city": "London"}
If you want to remove a key-value pair from the dictionary, you can use the del
keyword.
del student["grade"] # Removing the grade key-value pair
After deleting, the dictionary will look like this:
print(student) # Output: {"name": "Alice", "age": 23, "city": "London"}
my_dict = {}
to create a dictionary.my_dict[key]
to access a value by key.my_dict[key] = new_value
to update a value.my_dict[new_key] = new_value
to add a new key-value pair.del my_dict[key]
to delete a key-value pair.Here’s an example of a dictionary for storing contact information:
contact_info = {
"name": "John",
"phone": "123-456-7890",
"email": "john@example.com"
}
We can update John's phone number or add his address using the same CRUD operations as shown earlier.
Dictionaries are a powerful and flexible way to store and manipulate data in Python. They allow you to easily access and modify information based on a unique key, which is great for many real-world use cases like storing user profiles, managing data, and much more. Once you're comfortable with dictionaries, you'll be able to organize your data efficiently and solve problems with ease!
⬅ Previous Topic
Python ListsNext Topic ⮕
Python TuplesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.