Python Dictionaries: "Brand" "Ford" "Model" "Mustang" "Year"
Python Dictionaries: "Brand" "Ford" "Model" "Mustang" "Year"
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Dictionary
Example
Ordered or Unordered?
As of Python version 3.7, dictionaries are ordered. In
Python 3.6 and earlier, dictionaries are unordered.
Changeable
Example
Dictionary Length
Example
Example
type()
<class 'dict'>
Example
Accessing Items
You can access the items of a dictionary by referring to
its key name, inside square brackets:
Example
Output
Mustang
Example
x = thisdict.get("year")
Get Keys
Example
Output
dict_keys(['brand', 'model', 'year'])
Get Values
The values() method will return a list of all the values
in the dictionary.
Example
Example
x = car.values()
car["year"] = 2020
Get Items
The items() method will return each item in a
dictionary, as tuples in a list.
Example
Output
dict_items([('brand', 'Ford'), ('model',
'Mustang'), ('year', 1964)])
Python - Change Dictionary Items
Change Values
You can change the value of a specific item by referring
to its key name:
Example
Output
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}
Update Dictionary
Example
Update the "year" of the car by using
the update() method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
output
Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Output
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964,
'color': 'red'}
Update Dictionary
Example
Removing Items
Output
{'brand': 'Ford', 'year': 1964}
Example
output
{'brand': 'Ford', 'model': 'Mustang'}
Example
Output
Example