Dictionaries

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 a dict_items object
  • keys(): returns the keys in a dictionary as a dict_keys object
  • values(): returns the values in a dictionary as a dict_values object
  • get(): returns the value for a given key, or a default value if the key does not exist
  • pop(): removes an item from a dictionary and returns its value
  • update(): updates a dictionary with the key-value pairs from another dictionary
  • clear(): 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..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete