Table of Contents
Python dictionaries are a collection of changeable indexed, and not ordered elements. Similar to the Python sets, dictionaries us the curly brackets, but they contain keys and values. A Python dictionary is a mapping of unique keys to values, which are mutable. Mutable means that the Python dictionaries are able to be changed. Thee values that are referenced to the key in the Python dictionary can point to any Python value.
Python Dictionary Methods
Python Dictionary Creation
Creating a dictionary in Python can be completed by using curly brackets and separating the keys and values with a semicolon.
Input:
lambo_car = {"brand" : "Lamborghini", "model" : "Gallardo", "color" : "Yellow", "type" : "Coupe"} print(lambo_car)
Output:
{'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe'}
Python Dictionary Access
Python allows the access to the dictionary by referring to the keys and referencing the key name inside square brackets following the dictionary name.
Input:
lambo_model = lambo_car["model"] print(f"The model of the car is a {lambo_model}.")
Output:
The model of the car is a Gallardo.
get() method
Another method to getting the value from a key in a dictionary is using the get() method.
Input:
lambo_model = lambo_car.get("model") print(f"The model of the car is a {lambo_model}.")
Output:
The model of the car is a Gallardo.
Python Changing Dictionary Value
Changing a value in a Python dictionary can be completed by specifying an item to the key name.
Input:
lambo_car["color"] = "Red" lambo_color = lambo_car.get("color") print(f"The color is now changed from Yellow to {lambo_color}.")
Output:
The color is now changed from Yellow to Red.
Python Dictionary Looping
In order to locate a value in a Python Dictionary, a for loop can return the value from the keys of the dictionary.
Input:
for x in lambo_car: print(f"Dictionary Key: {x} ")
Output:
Dictionary Key: brand Dictionary Key: model Dictionary Key: color Dictionary Key: type
If one is looking to print all of the values of the lambo_car dictionary, this can be completed by specifying the dictionary name followed with [x].
Input:
for x in lambo_car: print(f"Dictionary Value: {lambo_car[x]} ")
Output:
Dictionary Value: Lamborghini Dictionary Value: Gallardo Dictionary Value: Red Dictionary Value: Coupe
values() method
Values can also be looped and printed by using the values() function to return the dictionary values.
Input:
for x in lambo_car.values(): print(f"Dictionary Value: {x} ")
Output:
Dictionary Value: Lamborghini Dictionary Value: Gallardo Dictionary Value: Red Dictionary Value: Coupe
items() method
If one is looking to loop the keys and values, the items() method will be very useful.
Input:
for x, y in lambo_car.items(): print(f"Dictionary Key: {x} - Dictionary Value: {y} ")
Output:
Dictionary Key: brand - Dictionary Value: Lamborghini Dictionary Key: model - Dictionary Value: Gallardo Dictionary Key: color - Dictionary Value: Red Dictionary Key: type - Dictionary Value: Coupe
Python Dictionary Validating Item
Python set item validating will determine if an item is in the list by using the keyword, in.
if…else Example 1
Input:
if "model" in lambo_car: print("Yes, model is one of the keys in the lambo_car dictionary.") else: print("No, model is one of the keys in the lambo_car dictionary.")
Output:
Yes, model is one of the keys in the lambo_car dictionary.
if…else Example 2
Input:
if "horsepower" in lambo_car: print("Yes, horsepower is one of the keys in the lambo_car dictionary.") else: print("No, horsepower is one of the keys in the lambo_car dictionary.")
Output:
No, horsepower is one of the keys in the lambo_car dictionary.
Python Dictionary Length
The internal method, len() will determine the length of the key-value pairs of a dictionary.
Input:
print(len(lambo_car))
Output:
4
Python Adding Dictionary Item
In order to add a new key-pair value in a dictionary, a new index key and a value must be assigned.
Input:
lambo_car["top speed"] = 203 lambo_top_speed = lambo_car.get("top speed") print(f"The top speed of a Gallardo is {lambo_top_speed}.")
Output:
The top speed of a Gallardo is 203.
Python Dictionary Item Removal
There are multiple methods to remove an item from a dictionary with a key name. Below are the methods:
- pop()
- popitem()
- del
pop()
The method pop() will remove the item with a specified key name.
Input:
print(lambo_car) lambo_car.pop("color") print(f"nRemoved the color key-value pair from the dictionary: {lambo_car}")
Output:
{'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Red', 'type': 'Coupe', 'top speed': 203} Removed the color key-value pair from the dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'type': 'Coupe', 'top speed': 203}
popitem()
The method popitem() will remove the last inserted item.
Input:
print(lambo_car) lambo_car.popitem() print(f"nRemoved the last item from the dictionary: {lambo_car}")
Output:
{'brand': 'Lamborghini', 'model': 'Gallardo', 'type': 'Coupe', 'top speed': 203} Removed the last item from the dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'type': 'Coupe'}
del method
The del keyword will remove the key-value pair with the specified key name.
Input:
print(lambo_car) del lambo_car["type"] print(f"nRemoved type from the dictionary: {lambo_car}")
Output:
{'brand': 'Lamborghini', 'model': 'Gallardo', 'type': 'Coupe'} Removed type from the dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo'}
Python Dictionary Copying
Python dictionary copying can be executed by using the copy() method or the dict() method. In Python, copying a dictionary by reference a variable1 to variable2 will only reference variable1.
copy() Method
The copy() method produces an identical copy of the dictionary.
Input:
old_lambo_car = {"brand" : "Lamborghini", "model" : "Gallardo", "color" : "Yellow", "type" : "Coupe"} new_lambo_car = old_lambo_car.copy() print(f"Old dictionary: {new_lambo_car}") print(f"Copied old dictionary: {new_lambo_car}")
Output:
Old dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe'} Copied old dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe'}
dict() Method
The dict() method produces an identical copy of the dictionary, such as the copy() method.
Input:
old_lambo_car = {"brand" : "Lamborghini", "model" : "Gallardo", "color" : "Yellow", "type" : "Coupe"} new_lambo_car = dict(old_lambo_car) print(f"Old dictionary: {new_lambo_car}") print(f"Copied old dictionary: {new_lambo_car}")
Output:
Old dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe'} Copied old dictionary: {'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe'}
Python dict() Constructor
The dict() constructor method to create a new dictionary.
Input:
constructor_lambo_car = dict(brand="Lamborghini", model="Gallardo", color="Yellow", type="Coupe", horsepower=203) print(f"dict() constructor list: {constructor_lambo_car}")
Output:
dict() constructor list: {'brand': 'Lamborghini', 'model': 'Gallardo', 'color': 'Yellow', 'type': 'Coupe', 'horsepower': 203}