0
Brackets vs Get function
Is there a difference in using brackets over the .get function? The output is still the same. Eg: pairs = {1: "apple", "orange": [2, 3, 4], True: False, None: "True", } print(pairs["orange"]) #can be print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(12345, "not in dictionary"))
1 Resposta
+ 1
get return none when element is not found in dictionary while brackets will raise keyerror if element is not present into dictionary.
Run below code to make it clear.
pairs = {1: "apple",
'orange': [2, 3, 4],
True: False,
None: "True",
}
print(pairs['orange'])
print(pairs.get('orange'))
print(pairs.get(7))
print(pairs['saga'])