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

Course Content

Python Data Structures

Python Data Structures

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

bookCreating a Dictionary

The basic syntax for creating a dictionary is as follows:

  • Keys: must be immutable (e.g., strings, numbers, tuples). A list or another dictionary cannot be the key;
  • Values: can be any data type (e.g., strings, numbers, lists, other dictionaries).

Imagine you are managing a library and want to store information about a book. Here's how you can create a dictionary to represent the book's details:

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

This dictionary uses strings as keys to store information about the book.

Dictionaries allow keys to be any immutable data type. For instance, you can use numbers as keys to create an index of library shelves:

12
shelves = {1: "Classics", 2: "Science Fiction", 3: "Mystery", 4: "Non-fiction"} print(shelves)
copy

In this example, numbers are keys to map genres to specific library shelves.

Note

An immutable data type is a type of data that cannot be changed after creation. Examples include strings, numbers, and tuples.

What happens if keys are duplicated in a dictionary?

In Python, dictionary keys must be unique. If duplicate keys are provided when creating or updating a dictionary, the last occurrence of the key will overwrite the previous one. This means the dictionary will only retain the most recent value associated with that key.

12
book = {"title": "1984", "author": "George Orwell", "title": "Animal Farm"} print(book) # Output: {'title': 'Animal Farm', 'author': 'George Orwell'}
copy

In this case, the second "title" key with the value "Animal Farm" overwrites the first "title" key.

Task
test

Swipe to show code editor

Create a dictionary named library to represent a small library. The dictionary should have three keys: title, author, and year. Each key should map to a specific value of your choice.

Decide the values for each key:

  • title: the name of the book.
  • author: the name of the writer.
  • year: the year the book was published.

Use curly braces {} to create the dictionary. Each key-value pair is written as key: value, separated by commas.

Here is a quick idea: "1984", "George Orwell", 1949.

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

bookCreating a Dictionary

The basic syntax for creating a dictionary is as follows:

  • Keys: must be immutable (e.g., strings, numbers, tuples). A list or another dictionary cannot be the key;
  • Values: can be any data type (e.g., strings, numbers, lists, other dictionaries).

Imagine you are managing a library and want to store information about a book. Here's how you can create a dictionary to represent the book's details:

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

This dictionary uses strings as keys to store information about the book.

Dictionaries allow keys to be any immutable data type. For instance, you can use numbers as keys to create an index of library shelves:

12
shelves = {1: "Classics", 2: "Science Fiction", 3: "Mystery", 4: "Non-fiction"} print(shelves)
copy

In this example, numbers are keys to map genres to specific library shelves.

Note

An immutable data type is a type of data that cannot be changed after creation. Examples include strings, numbers, and tuples.

What happens if keys are duplicated in a dictionary?

In Python, dictionary keys must be unique. If duplicate keys are provided when creating or updating a dictionary, the last occurrence of the key will overwrite the previous one. This means the dictionary will only retain the most recent value associated with that key.

12
book = {"title": "1984", "author": "George Orwell", "title": "Animal Farm"} print(book) # Output: {'title': 'Animal Farm', 'author': 'George Orwell'}
copy

In this case, the second "title" key with the value "Animal Farm" overwrites the first "title" key.

Task
test

Swipe to show code editor

Create a dictionary named library to represent a small library. The dictionary should have three keys: title, author, and year. Each key should map to a specific value of your choice.

Decide the values for each key:

  • title: the name of the book.
  • author: the name of the writer.
  • year: the year the book was published.

Use curly braces {} to create the dictionary. Each key-value pair is written as key: value, separated by commas.

Here is a quick idea: "1984", "George Orwell", 1949.

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

bookCreating a Dictionary

The basic syntax for creating a dictionary is as follows:

  • Keys: must be immutable (e.g., strings, numbers, tuples). A list or another dictionary cannot be the key;
  • Values: can be any data type (e.g., strings, numbers, lists, other dictionaries).

Imagine you are managing a library and want to store information about a book. Here's how you can create a dictionary to represent the book's details:

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

This dictionary uses strings as keys to store information about the book.

Dictionaries allow keys to be any immutable data type. For instance, you can use numbers as keys to create an index of library shelves:

12
shelves = {1: "Classics", 2: "Science Fiction", 3: "Mystery", 4: "Non-fiction"} print(shelves)
copy

In this example, numbers are keys to map genres to specific library shelves.

Note

An immutable data type is a type of data that cannot be changed after creation. Examples include strings, numbers, and tuples.

What happens if keys are duplicated in a dictionary?

In Python, dictionary keys must be unique. If duplicate keys are provided when creating or updating a dictionary, the last occurrence of the key will overwrite the previous one. This means the dictionary will only retain the most recent value associated with that key.

12
book = {"title": "1984", "author": "George Orwell", "title": "Animal Farm"} print(book) # Output: {'title': 'Animal Farm', 'author': 'George Orwell'}
copy

In this case, the second "title" key with the value "Animal Farm" overwrites the first "title" key.

Task
test

Swipe to show code editor

Create a dictionary named library to represent a small library. The dictionary should have three keys: title, author, and year. Each key should map to a specific value of your choice.

Decide the values for each key:

  • title: the name of the book.
  • author: the name of the writer.
  • year: the year the book was published.

Use curly braces {} to create the dictionary. Each key-value pair is written as key: value, separated by commas.

Here is a quick idea: "1984", "George Orwell", 1949.

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!

The basic syntax for creating a dictionary is as follows:

  • Keys: must be immutable (e.g., strings, numbers, tuples). A list or another dictionary cannot be the key;
  • Values: can be any data type (e.g., strings, numbers, lists, other dictionaries).

Imagine you are managing a library and want to store information about a book. Here's how you can create a dictionary to represent the book's details:

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

This dictionary uses strings as keys to store information about the book.

Dictionaries allow keys to be any immutable data type. For instance, you can use numbers as keys to create an index of library shelves:

12
shelves = {1: "Classics", 2: "Science Fiction", 3: "Mystery", 4: "Non-fiction"} print(shelves)
copy

In this example, numbers are keys to map genres to specific library shelves.

Note

An immutable data type is a type of data that cannot be changed after creation. Examples include strings, numbers, and tuples.

What happens if keys are duplicated in a dictionary?

In Python, dictionary keys must be unique. If duplicate keys are provided when creating or updating a dictionary, the last occurrence of the key will overwrite the previous one. This means the dictionary will only retain the most recent value associated with that key.

12
book = {"title": "1984", "author": "George Orwell", "title": "Animal Farm"} print(book) # Output: {'title': 'Animal Farm', 'author': 'George Orwell'}
copy

In this case, the second "title" key with the value "Animal Farm" overwrites the first "title" key.

Task
test

Swipe to show code editor

Create a dictionary named library to represent a small library. The dictionary should have three keys: title, author, and year. Each key should map to a specific value of your choice.

Decide the values for each key:

  • title: the name of the book.
  • author: the name of the writer.
  • year: the year the book was published.

Use curly braces {} to create the dictionary. Each key-value pair is written as key: value, separated by commas.

Here is a quick idea: "1984", "George Orwell", 1949.

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