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

Contenuti del Corso

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
Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 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
Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt