Dictionaries
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
In Python, a dictionary is a data structure that stores an unordered collection of items. Dictionaries are also known as associative arrays or hash maps. In a dictionary, each item has a key and a value. The key is used to access the item, and the value is the item itself. In Python, you can create a dictionary using curly braces {}
and the :
character to separate the keys and values. Here are some examples of creating dictionaries in Python:
# Create an empty dictionary
my_dict = {}
# Create a dictionary of numbers
numbers = {1: "one", 2: "two", 3: "three"}
# Create a dictionary of strings
colors = {"red": "#FF0000", "green": "#00FF00", "blue": "#0000FF"}
# Create a dictionary of mixed data types
mixed = {1: "one", "two": 2, 3.0: [3, 4, 5]}
As you can see, a dictionary can store items of any data type, including other dictionaries.
Accessing and Modifying Dictionary Items
In Python, you can access the items in a dictionary using the key. To access an item in a dictionary, use the square bracket notation []
and specify the key of the item you want to access. Here are some examples of accessing items in a dictionary:
# Access an item in a dictionary
print(numbers[1]) # Output: "one"
# Access a range of items in a dictionary (this will cause an error)
print(numbers[1:3]) # Output: TypeError: unhashable type: 'slice'
As you can see, you can access the items in a dictionary using the key, but you cannot access a range of items in a dictionary like you can with a list or a tuple. This is because dictionaries are unordered, so the items in a dictionary do not have a specific order or index.
To modify an item in a dictionary, use the square bracket notation []
and specify the key of the item you want to modify. Then, use the assignment operator =
to assign a new value to that item. Here are some examples of modifying items in a dictionary:
# Modify an item in a dictionary
numbers[1] = "uno"
print(numbers) # Output: {1: "uno", 2: "two", 3: "three"}
# Add an item to a dictionary
numbers[4] = "four"
print(numbers) # Output: {1: "uno", 2: "two", 3: "three", 4: "four"}
# Remove an item from a dictionary
del numbers[3]
print(numbers) # Output: {1: "uno", 2: "two", 4: "four"}
Common Dictionary Methods
In Python, dictionaries have a few built-in methods that allow you to manipulate and transform the items in a dictionary. Here are some common dictionary methods that you might find useful:
items()
: returns the key-value pairs in a dictionary as adict_items
objectkeys()
: returns the keys in a dictionary as adict_keys
objectvalues()
: returns the values in a dictionary as adict_values
objectget()
: returns the value for a given key, or a default value if the key does not existpop()
: removes an item from a dictionary and returns its valueupdate()
: updates a dictionary with the key-value pairs from another dictionaryclear()
: removes all the items from a dictionary
Here are some examples of using these dictionary methods:
# Get the key-value pairs in a dictionary
print(numbers.items()) # Output: dict_items([(1, 'one'), (2, 'two'), (3, 'three')])
# Get the keys in a dictionary
print(numbers.keys()) # Output: dict_keys([1, 2, 3])
# Get the values in a dictionary
print(numbers.values()) # Output: dict_values(['one', 'two', 'three'])
# Get the value for a given key
print(numbers.get(1)) # Output: "one"
print(numbers.get(4, "four")) # Output: "four"
# Remove an item from a dictionary
print(numbers.pop(2)) # Output: "two"
print(numbers) # Output: {1: "one", 3: "three"}
# Update a dictionary with the key-value pairs from another dictionary
numbers.update({4: "four", 5: "five"})
print(numbers) # Output: {1: "one", 3: "three", 4: "four", 5: "five"}
# Remove all the items from a dictionary
numbers.clear()
print(numbers) # Output: {}
As you can see, the dictionary methods in Python provide a way to manipulate and transform the items in a dictionary.
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete