["!0handles KeyError","iB'AD#(.B| 6647594}5aC3B","KeyError."," A dictionary can cause a KeyError to occur. This happens due to incorrect use of the dictionary. We can prevent the KeyError in most cases by using the get() method on the dictionary.","Example."," This program causes a KeyError to be thrown. The dictionary contains just three entries\u2014these have the keys \"a\", \"b\" and \"c\". We try to access a key \"d\" but it does not exist. And a KeyError is encountered.","Next,"," we trap the KeyError in a try-except construct. We print an error message in the except-block. And finally, after handling exceptions, we access key \"d\" with the get() method. This is safe. No exception is raised. ","Error ","error-python","A KeyError is avoidable"," in Python programs. We fix the problem by using a safe method, such as get(), instead of directly accessing the key. We can only directly access a key if we are sure it exists. ","If any direct accesses occur in your program, using a try-except block may be worthwhile if your code is new or untested.","Summary."," The KeyError is an avoidable exception in Python. It occurs when a dictionary is incorrectly used. We saw two ways to prevent this error. We used a try-except statement. And we replaced the value access with a get() method call. ","Dictionary ","dictionary-python"]

TSAOIAAI# CS_ dS- with three entries.AIShsSy{OaO : 1, ObO : 2, OcO : 3}IAA# Using the Sh directly can cause an error.AItry:ATS[(Shs[IOdOI])Aexcept IKeyErrorI:ATS[(OIKeyError encounteredIO)IAA# We use getSjsafely get a Sh.AIS[(Shs.IgetI(IOdOI))AAIAAIKeyError encounteredIANoneI

%iVBORw0KG;)NSUhEUg?AIw?ACMCAM?ACZHrEM)S1BMVEX::/6J3/8MH/+uv/21D/7bX/2Dz:fX/+OH/44H/9df/66n/4HP/3WL/5Y:8szV1dX7+/vGxsa2trb29va7u7vQ0NDy8vLLy8umG8PN?ACk0lEQVR4Xu3bCaoWQQwE4E6vs/5vc7v/SUVGLAxtnoidGrHrBB/FQCYNCb+Rl08vgRxYXt9eoaFb3qDhW5SGbYGGb4GGb4GGb4GGb0G+3Mjy8fP/ZJmWaZmWaVnijSy5xhtZKjR8CzR8i9LQLdDwLdDwLdDwLdDwLchxJ0v7ZyzTstzHsq013sSyrZgMRAso0BAt6TFuMjwbFp0thCSgDNbYlpqfQBmqsS2Ik0ZbWBrbYicvAzR/Zs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