Site icon Bernard Aybout's Blog – MiltonMarketing.com

Learn about Dictionaries datatype for Python

Learn about Dictionaries datatype for Python

Learn about Dictionaries datatype for Python

Lesson: Mastering Python Dictionaries – Learn about Dictionaries datatype for Python

Introduction to Python Dictionaries

A dictionary in Python is a collection of key-value pairs. Each key is unique and is used to access the corresponding value. Dictionaries provide an efficient way to store and retrieve data, where values can be any data type, but keys must be immutable types like strings, numbers, or tuples.

Creating a Dictionary

Method 1: Adding Entries Individually


phonebook = {}
phonebook["John"] = 555477566
phonebook["Jack"] = 555377264
phonebook["Jill"] = 555662781
print(phonebook)

Output:


{'John': 555477566, 'Jack': 555377264, 'Jill': 555662781}

Method 2: Initializing with Values


phonebook = {
    "John": 938477566,
    "Jack": 938377264,
    "Jill": 947662781
}
print(phonebook)

Output:


{'John': 938477566, 'Jack': 938377264, 'Jill': 947662781}

Accessing Values in a Dictionary


john_number = phonebook["John"]
print(john_number)

Output:


938477566

Modifying Values


phonebook["John"] = 555123456
print(phonebook)

Output:


{'John': 555123456, 'Jack': 938377264, 'Jill': 947662781}

Iterating Over Dictionaries

Dictionaries can be iterated over in various ways:

Iterating Over Keys


for name in phonebook:
    print(name)

Output:


John
Jack
Jill

Iterating Over Values


for number in phonebook.values():
    print(number)

Output:


555123456
938377264
947662781

Iterating Over Key-Value Pairs


for name, number in phonebook.items():
    print(f"Phone number of {name} is {number}")

Output:


Phone number of John is 555123456
Phone number of Jack is 938377264
Phone number of Jill is 947662781

Removing Entries

You can remove a specific key-value pair using the del statement or the .pop() method:

Using del Statement


del phonebook["John"]
print(phonebook)

Output:


{'Jack': 938377264, 'Jill': 947662781}

Using .pop() Method


phonebook.pop("John")
print(phonebook)

Output:


{'Jack': 938377264, 'Jill': 947662781}

Exercise

Task

1. Add “Jake” to the phonebook with the phone number 938273443.

2. Remove “Jill” from the phonebook.

Instructions

  1. Create a dictionary phonebook with the initial values.
  2. Add “Jake” to the dictionary.
  3. Remove “Jill” from the dictionary.
  4. Print the updated dictionary.
  5. Verify the changes.

Sample Assignment

Initial Code


phonebook = {
    "John": 938477566,
    "Jack": 938377264,
    "Jill": 947662781
}

# Your code here

# Testing code
if "Jake" in phonebook:
    print("Jake is listed in the phonebook.")
if "Jill" not in phonebook:
    print("Jill is not listed in the phonebook.")

Solution


# Initial phonebook
phonebook = {
    "John": 938477566,
    "Jack": 938377264,
    "Jill": 947662781
}

# Adding Jake and removing Jill
phonebook["Jake"] = 938273443
del phonebook["Jill"]

# Testing code
if "Jake" in phonebook:
    print("Jake is listed in the phonebook.")
if "Jill" not in phonebook:
    print("Jill is not listed in the phonebook.")

Expected Output


Jake is listed in the phonebook.
Jill is not listed in the phonebook.

Additional Concepts – Learn about Dictionaries datatype for Python

Nested Dictionaries

Dictionaries can contain other dictionaries, creating a nested structure:


contacts = {
    "John": {"phone": 938477566, "email": "john@example.com"},
    "Jack": {"phone": 938377264, "email": "jack@example.com"}
}
print(contacts)

Dictionary Comprehensions

Similar to list comprehensions, you can create dictionaries using dictionary comprehensions:


squares = {x: x*x for x in range(6)}
print(squares)

Output:



Additional Resources

Tools

You can use the Trinket.io online Python compiler to practice and run the code provided in this lesson.

Conclusion

Understanding dictionaries is essential for efficient data manipulation in Python. This lesson covered the basics and some advanced features of creating, modifying, iterating over, and deleting entries in dictionaries. Practice the exercises and explore the additional resources to deepen your understanding.


Related Videos:

Bernard Aybouts - Blog - MiltonMarketing.com

Related Posts:

Exit mobile version