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

Kurssisisältö

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Accessing 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
Tehtävä

Swipe to start coding

You are given a dictionary authors_books, where the key is the author and the value is a list of their book titles.

  • Initialize the variable keys as a list of the dictionary keys.
  • Initialize the variable all_books as a list of all available book titles.
  • Use a for loop to get the book lists by author.
  • Use a nested for loop and the append() method to fill the all_books list with all available books.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
toggle bottom row

book
Accessing 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
Tehtävä

Swipe to start coding

You are given a dictionary authors_books, where the key is the author and the value is a list of their book titles.

  • Initialize the variable keys as a list of the dictionary keys.
  • Initialize the variable all_books as a list of all available book titles.
  • Use a for loop to get the book lists by author.
  • Use a nested for loop and the append() method to fill the all_books list with all available books.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt