Creating a Dictionary in Python
=====================================
Python dictionaries are collections of key-value pairs. A dictionary's keys can be any type of data, including numbers and text, while its values can also be any type of data.
Creating a Dictionary
----------------------
To create a dictionary, we use the `dict` keyword followed by an equals sign and a set of curly brackets containing key-value pairs. For example:
```python
fruits = {"apple": "red", "banana": "yellow"}
```
In this example, we have created a dictionary called `fruits` with two key-value pairs: `"apple"` mapped to `"red"` and `"banana"` mapped to `"yellow"`.
Accessing Dictionary Keys and Values
---------------------------------------
Python dictionaries allow us to access their keys and values using square brackets. To get the value of a specific key, we simply use that key in square brackets:
```python
print(fruits["apple"]) # Output: red
# Using the get method:
print(fruits.get("banana")) # Output: yellow
```
The `get` method behaves similarly to accessing a key using square brackets but provides an optional default value if the key is not found in the dictionary.
Editing Dictionary Keys and Values
--------------------------------------
To edit or add new keys-value pairs to an existing dictionary, we use the same syntax as when creating a dictionary:
```python
fruits["orange"] = "orange"
fruits["melon"] = "green"
print(fruits) # Output: {"apple": "red", "banana": "yellow", "orange": "orange", "melon": "green"}
```
In this example, we have added two new key-value pairs to the `fruits` dictionary.
Retrieving Dictionary Keys and Values
-----------------------------------------
Python dictionaries also provide methods to retrieve their keys and values. To get all keys in a dictionary, we use the `.keys()` method:
```python
print(fruits.keys()) # Output: dict_keys(["apple", "banana", "orange", "melon"])
```
To get all values in a dictionary, we use the `.values()` method:
```python
print(fruits.values()) # Output: dict_values(["red", "yellow", "orange", "green"])
```
To get both keys and values as a list of tuples, we use the `.items()` method:
```python
print(fruits.items()) # Output: dict_items([("apple", "red"), ("banana", "yellow"), ("orange", "orange"), ("melon", "green")])
```
Checking if a Key Exists in a Dictionary
---------------------------------------------
To check if a specific key exists in a dictionary, we use the `in` keyword:
```python
print("apple" in fruits) # Output: True
print("carrot" in fruits) # Output: False
```
Adding New Key-Value Pairs to a Dictionary
---------------------------------------------
To add new key-value pairs to an existing dictionary, we can use the `.update()` method or simply add the new key-value pair using square brackets:
```python
fruits.update({"pear": "brown", "grape": "red"})
print(fruits) # Output: {"apple": "red", "banana": "yellow", "orange": "orange", "melon": "green", "pear": "brown", "grape": "red"}
```
Alternatively, we can add new key-value pairs using square brackets:
```python
fruits["pear"] = "brown"
fruits["grape"] = "red"
print(fruits) # Output: {"apple": "red", "banana": "yellow", "orange": "orange", "melon": "green", "pear": "brown", "grape": "red"}
```
Removing Key-Value Pairs from a Dictionary
---------------------------------------------
To remove key-value pairs from an existing dictionary, we can use the `.pop()` method or the `del` keyword:
```python
fruits.pop("melon")
print(fruits) # Output: {"apple": "red", "banana": "yellow", "orange": "orange", "grape": "red"}
del fruits["orange"]
print(fruits) # Output: {"apple": "red", "banana": "yellow", "grape": "red"}
```
Alternatively, we can use the `clear()` method to empty a dictionary:
```python
fruits.clear()
print(fruits) # Output: {}
```