Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Dictionaries | Other Data Types
Introduction to Python

bookDictionaries

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

# Syntax
dictionary = { <key> : <value>, <key> : <value>, <key> : <value> }

# Example
inventory = { "Apples": 30, "Oranges": 18 }

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note
Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation groceryItems = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", groceryItems["Milk"]) print("Price of Bread:", groceryItems["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

1. Which of the following statements about Python dictionaries are correct

2. Which of the following statements about Python dictionaries are true

question mark

Which of the following statements about Python dictionaries are correct

Select all correct answers

question mark

Which of the following statements about Python dictionaries are true

Select all correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 9

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookDictionaries

Swipe to show menu

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

# Syntax
dictionary = { <key> : <value>, <key> : <value>, <key> : <value> }

# Example
inventory = { "Apples": 30, "Oranges": 18 }

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note
Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation groceryItems = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", groceryItems["Milk"]) print("Price of Bread:", groceryItems["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

1. Which of the following statements about Python dictionaries are correct

2. Which of the following statements about Python dictionaries are true

question mark

Which of the following statements about Python dictionaries are correct

Select all correct answers

question mark

Which of the following statements about Python dictionaries are true

Select all correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 9
some-alt