Python Tutorials
Python File Handling
Python Modules
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Dictionaries are used to store key data values: value pairs.
The dictionary is a compiled * collection, which is flexible and does not allow duplicates.
From version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.
Dictionaries are written in italics, and have keys and numbers:
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Dictionary items are unique, flexible, and do not allow duplicates.
Dictionary items are presented with a key: in pairs of value, and can be referenced using a keyword.
Print the "brand" value of the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
From version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.
If we say that dictionaries are one thing, it means that things are in order, and that order is bound to change.
Disorder means the items do not have a specified order, you cannot refer to an item using the index.
Dictionaries are flexible, meaning we can change, add or remove items once the dictionary has been created.
Dictionaries cannot have two objects with the same key:
Duplicate values will overwrite existing values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
To determine how many items a dictionary contains, use the len()
function:
Print the number of items in the dictionary:
print(len(thisdict))
Prices for dictionary items can be for any type of data:
String, int, boolean, and list data types:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
From Python's point of view, dictionaries are defined as 'dictators':
<class 'dict'>
Print the data type of a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(type(thisdict))
There are four types of data collection in Python editing language:
* Set items do not change, but you can delete and / or add items whenever you wish.
** Starting with version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.
When choosing a type of collection, it is helpful to understand the characteristics of that type. Choosing the right type of specific data set may mean keeping the definition, and, it may mean increased efficiency or security.
You can access dictionary items by referring to its keyword, inside square brackets:
Get the value of the "model" key:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
There is also a method called get () that will give you the same result:
Get the value of the "model" key:
x = thisdict.get("model")
The keys () method will return a list of all the keys to the dictionary.
Get a list of the keys:
x = thisdict.keys()
A list of keywords is a dictionary view, which means that any changes made to the dictionary will be reflected in the key list.
Add a new item to the original dictionary, and see that the keys list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] =
"white"
print(x) #after the change
The values () method will return a list of all values to the dictionary.
Get a list of the values:
x = thisdict.values()
The price list is a dictionary view, which means that any changes made to the dictionary will be reflected in the price list.
Make a change in the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"]
= 2020
print(x) #after the change
Add a new item to the original dictionary, and see that the values list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["color"]
= "red"
print(x) #after the change
The items () method will return each item to the dictionary, such as tuples in the list.
Get a list of the key:value pairs
x = thisdict.items()
The returned list is a view of dictionary items, which means that any changes made to the dictionary will be reflected in the list of items.
Make a change in the original dictionary, and see that the items list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"]
= 2020
print(x) #after the change
Add a new item to the original dictionary, and see that the items list gets updated as well:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["color"]
= "red"
print(x) #after the change
To determine if a specific key exists in the dictionary use the internal keyword:
Check if "model" is present in the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is
one of the keys in the thisdict dictionary")