Python Dictionaries
A Dictionary is a collection of many items. The indices of dictionaries can use different data types. Indices for dictionaries are called keys and values. Key with its associated value is called key:value pair. For example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
Accessing Dictionary Items
You can access the dictionary items by referring to the key.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
a = dict1["fruit"]
print(a)
Output:
apple
Dictionary have also a get() method that will give the same output.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
a = dict1.get("fruit")
print(a)
Output:
apple
Changing Items
You can change the dictionary items by referring to the key.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
dict1["fruit"] = "banana"
print(dict1)
Output:
{'fruit': 'banana', 'color': 'red', 'size': 'small'}
Adding Items
You can add a new item in a dictionary using a new key-value pair.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
dict1["price"] = "25"
print(dict1)
Output:
{'fruit': 'apple', 'color': 'red', 'size': 'small', 'price': '25'}
Removing Items
You can remove a specified item by using the del statement.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
del dict1["size"]
print(dict1)
Output:
{'fruit': 'apple', 'color': 'red'}
You can also use the del statement to delete the entire dictionary.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
del dict1
print(dict1)
Output:
Traceback (most recent call last): File "C:\Users\Hp\Desktop\py\python.py", line 3, in <module> print(dict1) NameError: name 'dict1' is not defined
Note: Since the total set is deleted an error is raised in the output.
The clear() statement will empty the whole dictionary.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
dict1.clear()
print(dict1)
Output:
{}
Length of a Dictionary
You can define the length of a Dictionary using the len() method.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
print(len(dict1))
Output:
3
Checking Whether a Key Exists in a Dictionary
The in and not in operators can check whether a certain key exists in a dictionary.
Example:
dict1 = {"fruit": "apple", "color": "red", "size": "small"}
if "color" in dict1:
print("Color is red")
Output:
Color is red
Dictionary Methods
There are a set of built-in methods in python which you can use on Dictionary.
Method | Description |
---|---|
clear() | Removes all values from a dictionary |
copy() | Returns a copy of a dictionary |
fromkeys() | Returns a dictionary which contains the specified keys and value |
get() | Returns the value of a specified key |
items() | Returns a dictionary which contains a tuple for each key value pair |
key() | Returns a dictionary which contains the dictionary's keys |
pop() | Removes a value with the specified key |
setdefault() | Returns the specified key's value. If the key does not exist adds the key with the specified value. |
update() | Updates the dictionary with specified key-value pairs |
values() | Returns a list of all values from the dictionary |