u2_album = {
'title': 'The Joshua Tree',
'year': 1987,
'genre': 'Rock',
'tracks': ['Where the Streets Have No Name', 'I Still Haven\'t Found What I\'m Looking For', 'With or Without You', 'Bullet the Blue Sky', 'Running to Stand Still', 'Red Hill Mining Town', 'In God\'s Country', 'Trip Through Your Wires', 'One Tree Hill', 'Exit', 'Mothers of the Disappeared']
}
print(u2_album['title'])
print(u2_album['year'])
print(u2_album['track'])
If you try to run the above Python code, you will get a KeyError error.
The Joshua Tree
1987
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-25-be282156e277> in <cell line: 10>()
8 print(u2_album['title'])
9 print(u2_album['year'])
---> 10 print(u2_album['track'])
KeyError: 'track'
Why KeyError exception?
Python will throw a KeyError exception when a dictionary key is not found in the set of existing keys.
Fix:
If you look closely at the line where the error is thrown, we have made use of track which does not exist, its tracks.
Should be:
print(u2_album['tracks'])

Reference: https://docs.python.org/3/library/exceptions.html#KeyError
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Locate Notepad++ unsaved files backup location - NotepadPlusPlus
- Android Developers Bluetooth Tutorial - Android
- [Solution] AWS Java SDK S3 AmazonS3Exception InvalidAccessKeyId - AWS
- How to switch between sftp and Terminal shell - FTP
- Fix: error: could not find `Cargo.toml` in Users or any parent directory - Rust
- [git] fatal: your current branch 'main' does not have any commits yet - Git
- How to know Notepad++ is 32-bit or 64-bit Version - NotepadPlusPlus
- SharePoint - Use Today's Date Time in list view filter and calculated column - SharePoint