A dictionary is a collection of key-value pair elements {key: value} in Python. The keys in a dictionary must be unique and are used to access their corresponding values. The values in a dictionary can be of any data type such as string, integer, float, boolean, list, tuple, set, or data frame, and they do not have to be unique.
For example, you can use a dictionary to store a list of students and their grades. The keys in the dictionary would be the student names, and the values would be the grades. You can then use the keys to access the corresponding grades for each student.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict)
Output: {'Mike': 4.20, 'Lucy': 3.55, 'John': 3.75, 'Harry': 4.05}
Note: Like a Python set, a dictionary is an unordered and mutable data type in Python. This means that the items in a dictionary are not stored in a particular order, and you can add, remove, or change the items in the dictionary.
To access the value for a particular key in the dictionary, you can use the indexing operator "[]" which is nothing but square brackets.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict['Lucy'])
Output: 3.55
To add a new key-value pair element to an existing dictionary, you can use the assignment operator "=" which is nothing but an equals sign.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
test_dict['Ron'] = 3.95
print(test_dict)
Output: {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05, 'Ron': 3.95}
To remove a key-value pair element from an existing dictionary, you can use the pop() method. The pop() method removes the key-value pair from the dictionary and returns the value. If the key is not found in the dictionary, it returns a default value.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict.pop('Harry', 0.0))
print(test_dict)
Output: 4.05
Output: {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20}
print(test_dict.pop('Ron', 0.0))
print(test_dict)
Output: 0.0
Output: {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
Basic Functions and Methods for Dictionary
len(): the len() function is a Python built-in function that returns the number of elements (length) of an object. When applied to a dictionary, it returns the number of key-value pair elements in the dictionary.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(len(test_dict))
Output: 4
type(): the type() function in a Python built-in function that returns the type of an object. When applied to a dictionary, it returns the type 'dict'.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(type(test_dict))
Output: <class 'dict'>
test_dict.items(): the item() method of a dictionary is used to return a list of tuples containing key-value pair elements from the dictionary.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict)
print(test_dict.items())
Output: {'Mike': 4.20, 'Lucy': 3.55, 'John': 3.75, 'Harry': 4.05}
Output: [('Mike': 4.20), ('Lucy': 3.55), ('John': 3.75), ('Harry': 4.05)]
Note: Python returns a list of elements as tuples in the same order as they appear in the dictionary.
test_dict.get(): the get() method of a dictionary is used to return the value for the provided key, or a default value if the key is not found in the dictionary.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict.get('Lucy', 0.0))
Output: 3.55
print(test_dict.get('Ron', 0.0))
Output: 0.0
test_dict.keys(): the keys() method of a dictionary is used to return a list of keys available in the dictionary (ignoring the values of the dictionary). No arguments are required.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict)
print(test_dict.keys())
Output: {'Mike': 4.20, 'Lucy': 3.55, 'John': 3.75, 'Harry': 4.05}
Output: ['Mike', 'Lucy', 'John', 'Harry']
Note: Python returns a list of keys in the same order as they appear in the dictionary.
test_dict.values(): the value() method of a dictionary is used to return a list of values available in the dictionary (ignoring the keys of the dictionary). No arguments are required.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict)
print(test_dict.values())
Output: {'Mike': 4.20, 'Lucy': 3.55, 'John': 3.75, 'Harry': 4.05}
Output: [4.20, 3.55, 3.75, 4.05]
Note: Python returns a list of values in the same order as they appear in the dictionary.
test_dict.clear(): the clear() method of a dictionary is used to remove all the elements from an existing dictionary. No arguments are required.
test_dict = {'John': 3.75, 'Lucy': 3.55, 'Mike': 4.20, 'Harry': 4.05}
print(test_dict.clear())
Output: {}
the clear() method is often used to clear the dictionary for reuse.
Opmerkingen