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

Course Content

Python Data Structures

Python Data Structures

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

bookAccessing 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.

Task
test

Swipe to show code editor

Print the title of the book and the year it was published from the book dictionary. Use key indexing to retrieve the information.

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 2
toggle bottom row

bookAccessing 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.

Task
test

Swipe to show code editor

Print the title of the book and the year it was published from the book dictionary. Use key indexing to retrieve the information.

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 2
toggle bottom row

bookAccessing 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.

Task
test

Swipe to show code editor

Print the title of the book and the year it was published from the book dictionary. Use key indexing to retrieve the information.

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!

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.

Task
test

Swipe to show code editor

Print the title of the book and the year it was published from the book dictionary. Use key indexing to retrieve the information.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 2
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