Class-16 Python Dictionary
Class-16 Python Dictionary
DICTIONARIES
Python by Computer G
DICTIONARIES
A dictionary is a collection which is unordered, changeable and
indexed. In Python dictionaries are written with curly brackets,
and they have keys and values.
Example Create and print a dictionary:
thisdict = { "brand": "Ford",
"model": "Mustang",
"year": 1964 }
print(thisdict) # returns {'brand': 'Ford', 'model': 'Mustang', 'year':
1964}
ACCESSING ITEMS
You can access the items of a dictionary by referring to its
key name, inside square brackets:
Example Get the value of the "model" key:
x = thisdict["model"]
There is also a method called get() that will give you the
same result:
Example Get the value of the "model" key:
x = thisdict.get("model")
CHANGE VALUES
You can change the value of a specific item by referring to
its key name:
Example Change the "year" to 2018:
thisdict = { "brand": "Ford",
"model": "Mustang",
"year": 1964 }
thisdict["year"]=2019 # returns {'brand': 'Ford', 'model':
'Mustang', 'year': 2019}
CHECK IF KEY EXISTS
To determine if a specified key is present in a dictionary use the
in keyword:
Example 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")
DICTIONARY LENGTH
To determine how many items (key-value pairs) a dictionary
has, use the len() method.
Example Print the number of items in the dictionary:
print(len(thisdict))
ADDING ITEMS
Adding an item to the dictionary is done by using a new
index key and assigning a value to it:
Example
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
thisdict["color"] = "red"
print(thisdict)
REMOVING ITEMS
There are several methods to remove items from a dictionary:
The pop() method removes the item with the specified key
name:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
thisdict.pop("model")
print(thisdict)
REMOVE ITEM
Example The popitem() method removes the last
inserted item
thisdict = { "brand": "Ford",
"model": "Mustang",
"year": 1964 }
thisdict.popitem()
print(thisdict)
DEL KEYWORD
Example The del keyword removes the item with the
specified key name:
thisdict = { "brand": "Ford", "model": "Mustang", "year":
1964 }
del thisdict["model"]
print(thisdict)
DEL KEYWORD
Example The del keyword can also delete the dictionary
completely:
thisdict = { "brand": "Ford", "model": "Mustang", "year":
1964 }
del thisdict
CLEAR()
Example The clear() keyword empties the dictionary:
thisdict = { "brand": "Ford", "model": "Mustang", "year":
1964 }
thisdict.clear()
print(thisdict)
COPY A DICTIONARY
You cannot copy a dictionary simply by typing dict2
= dict1, because: dict2 will only be a reference to
dict1, and changes made in dict1 will automatically
also be made in dict2.
COPY A DICTIONARY
There are ways to make a copy, one way is to use the built-
in Dictionary method copy().
Example Make a copy of a dictionary with the copy()
method:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
mydict = thisdict.copy()
print(mydict)
COPY A DICTIONARY
Another way to make a copy is to use the built-in method
dict().
Example Make a copy of a dictionary with the dict()
method:
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
mydict = dict(thisdict)
print(mydict)
Thanks For Watching