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

Kursinnehåll

Python Data Structures

Python Data Structures

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

book
Accessing Dictionary Values

Let's build a dictionary to represent a book in a library:

12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

This dictionary includes three key-value pairs:

In a dictionary, you can retrieve an item by referencing its key. Here's an example of accessing the publication year of the book:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} publication_year = book["year"] print(publication_year) # Output: 1813
copy

In the code provided:

  • Python searches for the key "year" in the book dictionary;
  • It retrieves the value associated with the key (1813) and assigns it to the variable publication_year.

The value is then printed, demonstrating how keys allow quick and specific access to dictionary values.

Uppgift

Swipe to start coding

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

  • Initialize the variable kings_books as a list of Stephen King's books.
  • Initialize the variable shekspeares_books as a list of Shakespeare's books.
  • You should initialize the variables by getting the data from the dictionary using the key.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
toggle bottom row

book
Accessing Dictionary Values

Let's build a dictionary to represent a book in a library:

12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

This dictionary includes three key-value pairs:

In a dictionary, you can retrieve an item by referencing its key. Here's an example of accessing the publication year of the book:

1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} publication_year = book["year"] print(publication_year) # Output: 1813
copy

In the code provided:

  • Python searches for the key "year" in the book dictionary;
  • It retrieves the value associated with the key (1813) and assigns it to the variable publication_year.

The value is then printed, demonstrating how keys allow quick and specific access to dictionary values.

Uppgift

Swipe to start coding

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

  • Initialize the variable kings_books as a list of Stephen King's books.
  • Initialize the variable shekspeares_books as a list of Shakespeare's books.
  • You should initialize the variables by getting the data from the dictionary using the key.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt