Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing Dictionary Keys | Dictionary
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookAccessing Dictionary Keys

To access the keys of a dictionary in Python, you can use the keys() method. This returns a view object that displays all the keys in the dictionary.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
copy

Iterating Through Keys

You can iterate through the keys in a dictionary using a for loop:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
copy

Checking for the Existence of a Key

Use the in keyword to check if a specific key exists in the dictionary:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
copy
Task
test

Swipe to show code editor

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookAccessing Dictionary Keys

To access the keys of a dictionary in Python, you can use the keys() method. This returns a view object that displays all the keys in the dictionary.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
copy

Iterating Through Keys

You can iterate through the keys in a dictionary using a for loop:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
copy

Checking for the Existence of a Key

Use the in keyword to check if a specific key exists in the dictionary:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
copy
Task
test

Swipe to show code editor

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookAccessing Dictionary Keys

To access the keys of a dictionary in Python, you can use the keys() method. This returns a view object that displays all the keys in the dictionary.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
copy

Iterating Through Keys

You can iterate through the keys in a dictionary using a for loop:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
copy

Checking for the Existence of a Key

Use the in keyword to check if a specific key exists in the dictionary:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
copy
Task
test

Swipe to show code editor

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

To access the keys of a dictionary in Python, you can use the keys() method. This returns a view object that displays all the keys in the dictionary.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
copy

Iterating Through Keys

You can iterate through the keys in a dictionary using a for loop:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
copy

Checking for the Existence of a Key

Use the in keyword to check if a specific key exists in the dictionary:

123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
copy
Task
test

Swipe to show code editor

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt