Build Function .Py
Build Function .Py
(technically, methods) that allow you to manipulate and interact with dictionary data. Here are the
main built-in functions that are applicable to dictionaries:
1. len()
Returns the number of items (key-value pairs) in the dictionary.
print(len(person)) # Output: 3
type()
str()
Converts the dictionary to its string representation
dict()
This is the built-in Python constructor to create a dictionary.
max()
Returns the maximum key in the dictionary. By default, it compares the keys (not values).
print(max(data)) # Output: 3
min()
Returns the minimum key in the dictionary.
print(min(data)) # Output: 1
sorted()
Returns a sorted list of the dictionary's keys.
all()
Returns True if all keys in the dictionary are truthy, or if the dictionary is empty.
data = {1: "a", 2: "b", 0: "c"} # 0 is falsy
any()
Returns True if at least one key in the dictionary is truthy. If the dictionary is empty, it returns False.
sum()
Returns the sum of the keys (only works if keys are numeric).
print(len(person)) # Output: 3
print(max(data)) # Output: 3
print(min(data)) # Output: 1
print(sum(data)) # Output: 6
Example Summary:
print(len(person)) # Output: 3
print(max(data)) # Output: 3
# Get the minimum key
print(min(data)) # Output: 1
print(sum(data)) # Output: 6