Dictionary
A dictionary is an unordered, mutable, and indexed collection. In Python, dictionaries are written in braces and have keys and values.
an actual example
Creates and prints a dictionary:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } print(thisdict)
running example
Visiting projects
You can access a dictionary item by referring to its key name in square brackets:
an actual example
Gets the value of the "model" key:
x = thisdict["model"]
running example
There is also a method called get() that will give you the same result:
an actual example
Gets the value of the "model" key:
x = ("model")
running example
value change
You can change the value of a specific item by referencing its key name:
an actual example
Change "year" to 2019:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["year"] = 2019
running example
Iterative dictionary
You can use a for loop to traverse the dictionary.
When looping through a dictionary, the return value is the key of the dictionary, but there are methods that return values.
an actual example
Prints all key names in the dictionary one by one:
for x in thisdict: print(x)
running example
an actual example
Prints all values in the dictionary one by one:
for x in thisdict: print(thisdict[x])
running example
an actual example
You can also use the values() function to return dictionary values:
for x in (): print(x)
running example
an actual example
Iterate over the keys and values by using the items() function:
for x, y in (): print(x, y)
running example
Check if the key exists
To determine if the specified key exists in the dictionary, use the in keyword:
an actual example
Checks for the presence of "model" in the dictionary:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } if "model" in thisdict: print("Yes, 'model' is one of the keys in the thisdict dictionary")
running example
Dictionary length
To determine how many items (key-value pairs) the dictionary has, use the len() method.
an actual example
Prints the number of items in the dictionary:
print(len(thisdict))
running example
Add Project
Items can be added to the dictionary by using a new index key and assigning a value to it:
an actual example
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["color"] = "red" print(thisdict)
running example
Deleting items
There are several ways to remove items from the dictionary:
an actual example
The pop() method deletes the item with the specified key name:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } ("model") print(thisdict)
running example
an actual example
The popitem() method deletes the last inserted item (in versions prior to 3.7, it deletes random items):
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } () print(thisdict)
running example
an actual example
The del keyword deletes the item with the specified key name:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } del thisdict["model"] print(thisdict)
running example
an actual example
The del keyword also removes the dictionary entirely:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } del thisdict print(thisdict) #this causes an error because "thisdict" no longer exists.
running example
an actual example
clear() keyword clears the dictionary:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } () print(thisdict)
running example
Copy Dictionary
You can't copy the dictionary by typing dict2 = dict1 because: dict2 is just a reference to dict1 and changes in dict1 will automatically be made in dict2 as well.
There are a number of ways to make a copy, one way is to use the built-in dictionary method copy().
an actual example
Use the copy() method to copy the dictionary:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = () print(mydict)
running example
Another way to make a copy is to use the built-in method dict().
an actual example
Use the dict() method to create a copy of the dictionary:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = dict(thisdict) print(mydict)
running example
nested dictionary
Dictionaries can also contain many dictionaries, which are called nested dictionaries.
an actual example
Creates a dictionary containing three dictionaries:
myfamily = { "child1" : { "name" : "Phoebe Adele", "year" : 2002 }, "child2" : { "name" : "Jennifer Katharine", "year" : 1996 }, "child3" : { "name" : "Rory John", "year" : 1999 } }
running example
{'child1': {'name': 'Phoebe Adele', 'year': 2002}, 'child2': {'name': 'Jennifer Katharine', 'year': 1996}, 'child3': {'name': 'Rory John', 'year': 1999}}
Or, if you want to nest three dictionaries that already exist as dictionaries:
an actual example
Create three dictionaries and then create a dictionary that contains the other three dictionaries:
child1 = { "name" : "Phoebe Adele", "year" : 2002 } child2 = { "name" : "Jennifer Katharine", "year" : 1996 } child3 = { "name" : "Rory John", "year" : 1999 } myfamily = { "child1" : child1, "child2" : child2, "child3" : child3 }
running example
{'child1': {'name': 'Phoebe Adele', 'year': 2002}, 'child2': {'name': 'Jennifer Katharine', 'year': 1996}, 'child3': {'name': 'Rory John', 'year': 1999}}
dict() constructor
A new dictionary can also be created using the dict() constructor:
an actual example
thisdict = dict(brand="Porsche", model="911", year=1963) # Note that the keyword is not a string literal # Note the use of an equal sign instead of a colon to assign values print(thisdict)
running example
dictionary method
Python provides a set of built-in methods that can be used on dictionaries.
To this point this tutorial on Python Getting Started (XV) Python dictionary article is introduced to this, more related Python dictionary content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!