Created
July 30, 2019 15:25
-
-
Save jmilagroso/6ec6a434043aaf50dd40dbf58505afc6 to your computer and use it in GitHub Desktop.
Python - Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
print(thisdict) | |
# You can access the items of a dictionary by referring to its key name, inside square brackets: | |
x = thisdict["model"] | |
x = thisdict.get("model") | |
# You can change the value of a specific item by referring to its key name: | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
thisdict["year"] = 2018 | |
# You can loop through a dictionary by using a for loop. | |
# When looping through a dictionary, the return value are the keys of the dictionary, | |
# but there are methods to return the values as well. | |
for x in thisdict: | |
print(x) # prints all the keys one by one | |
print(thisdict[x]) # prints all values one by one | |
for x in thisdict.values(): | |
print(x) # prints all values one by one | |
# Loop through both keys and values, by using the items() function: | |
for x, y in thisdict.items(): | |
print(x, y) | |
# To determine if a specified key is present in a dictionary use the in keyword: | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
if "model" in thisdict: | |
print("Yes, 'model' is one of the keys in the thisdict dictionary") | |
# To determine how many items (key-value pairs) a dictionary has, use the len() method. | |
print(len(thisdict)) | |
# Adding an item to the dictionary is done by using a new index key and assigning a value to it: | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
thisdict["color"] = "red" | |
print(thisdict) | |
# 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) | |
# The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead): | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
thisdict.popitem() | |
print(thisdict) | |
# The del keyword removes the item with the specified key name: | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
del thisdict["model"] | |
print(thisdict) | |
# The del keyword can also delete the dictionary completely: | |
del thisdict | |
# The clear() keyword empties the dictionary: | |
thisdict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
thisdict.clear() | |
print(thisdict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment