We’ll be creating a new script that parses a dictionary file into readable text. In the unit, we used the capitals of countries as our dictionary source. For this activity, we’ll be choosing and creating two new dictionaries to pull data from.

Step 1: Create Your Dictionaries

Choose two new themes for your new dictionaries. Here are a few examples:

Games and their developers
Top songs and their performing artists
The winners of a yearly tournament or competition

As a reminder, you can define a dictionary with the following code:

# creates a new dictionary
dictionaryName = {
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
}

Create a new file in PythonAnywhere. Add AT LEAST 10 keys and corresponding values to two different dictionaries.

Add comments to your code to explain what each line or section of your code is doing.
Step 2: Print Your Output

Now comes the fun part—translating a dictionary into a readable format in code. For each dictionary, write an output of a key and value into a printed string, Then, separately print the output of the contents of all keys and all values.

The contents of the output should read something like this:

The video game The Legend of Zelda was developed by Nintendo.
The current games in this dictionary are ['Halo', 'The Legend of Zelda', 'God of War', 'MineCraft', 'Portal']
The current developers in this dictionary are ['343 Industries', 'Nintendo', 'Santa Monica Studio', 'Mojang', 'Valve']

Step 3: Compare Data Types

Now that you have completed this activity using dictionaries, what other data types might we be able to use for this scenario?

In a new word processing document or in a deliverable of your choice (audio or video recording, etc.), describe AT LEAST FOUR data container types, explain whether they would work for this scenario, and if so, describe how the process would differ.

Next, create a second script in PythonAnywhere and attempt to convert your dictionary into another data type. The output should remain identical, but the data type and code should change.

Explain your process converting to this secondary data type. Explain why this new data type was easier or more difficult to use in this scenario.

# Creates a dictionary for video games and their developers
games_dictionary = {
"The Legend of Zelda: Breath of the Wild": "Nintendo",
"The Witcher 3: Wild Hunt": "CD Projekt Red",
"Dark Souls": "FromSoftware",
"God of War": "Santa Monica Studio",
"Minecraft": "Mojang Studios",
"Fortnite": "Epic Games",
"Red Dead Redemption 2": "Rockstar Games",
"Hollow Knight": "Team Cherry",
"Overwatch": "Blizzard Entertainment",
"Celeste": "Maddy Makes Games"
}

# Creates a dictionary for popular movies and their directors
movies_dictionary = {
"Inception": "Christopher Nolan",
"The Shawshank Redemption": "Frank Darabont",
"Pulp Fiction": "Quentin Tarantino",
"The Godfather": "Francis Ford Coppola",
"The Dark Knight": "Christopher Nolan",
"Schindler's List": "Steven Spielberg",
"Fight Club": "David Fincher",
"Forrest Gump": "Robert Zemeckis",
"The Matrix": "Lana and Lilly Wachowski",
"Parasite": "Bong Joon-ho"
}

# Print the content of the games dictionary in a readable format
print("Video Games and their Developers:")
for game, developer in games_dictionary.items():
print(f"{game} was developed by {developer}")

# Print the content of the movies dictionary in a readable format
print("\nPopular Movies and their Directors:")
for movie, director in movies_dictionary.items():
print(f"{movie} was directed by {director}")

1 answer

Sure! Below is the full Python code that executes your instructions. I will also explain the data types and how to convert the dictionary to another format afterward.

Python Code

# Creates a dictionary for video games and their developers
games_dictionary = {
    "The Legend of Zelda: Breath of the Wild": "Nintendo",
    "The Witcher 3: Wild Hunt": "CD Projekt Red",
    "Dark Souls": "FromSoftware",
    "God of War": "Santa Monica Studio",
    "Minecraft": "Mojang Studios",
    "Fortnite": "Epic Games",
    "Red Dead Redemption 2": "Rockstar Games",
    "Hollow Knight": "Team Cherry",
    "Overwatch": "Blizzard Entertainment",
    "Celeste": "Maddy Makes Games"
}

# Creates a dictionary for popular movies and their directors
movies_dictionary = {
    "Inception": "Christopher Nolan",
    "The Shawshank Redemption": "Frank Darabont",
    "Pulp Fiction": "Quentin Tarantino",
    "The Godfather": "Francis Ford Coppola",
    "The Dark Knight": "Christopher Nolan",
    "Schindler's List": "Steven Spielberg",
    "Fight Club": "David Fincher",
    "Forrest Gump": "Robert Zemeckis",
    "The Matrix": "Lana and Lilly Wachowski",
    "Parasite": "Bong Joon-ho"
}

# Print the content of the games dictionary in a readable format
print("Video Games and their Developers:")
for game, developer in games_dictionary.items():
    print(f"{game} was developed by {developer}")

# Print the content of the movies dictionary in a readable format
print("\nPopular Movies and their Directors:")
for movie, director in movies_dictionary.items():
    print(f"{movie} was directed by {director}")

# Print the current games and their developers in list format
print("\nThe current games in this dictionary are", list(games_dictionary.keys()))
print("The current developers in this dictionary are", list(games_dictionary.values()))

# Print the current movies and their directors in list format
print("\nThe current movies in this dictionary are", list(movies_dictionary.keys()))
print("The current directors in this dictionary are", list(movies_dictionary.values()))

Output Explanation

The output from running the above code will provide a list of video games along with their developers and a separate list of popular movies with their directors.

Data Container Types

Now let's discuss some other data container types and how they would work in this scenario:

  1. Lists:

    • Description: Lists are ordered collections of items.
    • Usage: You could create a list of tuples, where each tuple contains a game and its developer.
    • Process Difference: Accessing items would be position-based instead of key-based, which could be less intuitive than dictionaries for this scenario.
  2. Sets:

    • Description: Sets are unordered collections that do not allow duplicate entries.
    • Usage: Sets could be used to store unique game titles or developers, but not to map them.
    • Process Difference: Since there's no key-value pairing, this wouldn't work for our needs as we need to associate developers with games.
  3. Named Tuples:

    • Description: A named tuple assigns names to the various fields in a tuple.
    • Usage: You could use named tuples to create objects representing a game and its developer.
    • Process Difference: Named tuples would provide clearer code, but the readability might decrease since fields need to be accessed by name from each record.
  4. Data Classes (Python 3.7+):

    • Description: Data classes automatically generate special methods like __init__() and __repr__() for class definitions.
    • Usage: A data class could replace the dictionaries to define a Game and Movie class with attributes for title and developer/director.
    • Process Difference: This would help create more structured data representations, but the simplicity of dictionary operations (like quick lookups) would be sacrificed.

Second Script Example

To demonstrate converting dictionaries to another format, we can use lists of tuples:

# Convert games_dictionary to a list of tuples
games_list = list(games_dictionary.items())

# Print the content of the converted games list in a readable format
print("\nVideo Games List:")
for game, developer in games_list:
    print(f"{game} was developed by {developer}")

# Convert movies_dictionary to a list of tuples
movies_list = list(movies_dictionary.items())

# Print the content of the converted movies list in a readable format
print("\nPopular Movies List:")
for movie, director in movies_list:
    print(f"{movie} was directed by {director}")

Process Explanation

In this new example, I've converted the dictionaries into lists of tuples. While printing the data remains similar (readable string format), the underlying data structure is now a list rather than a dictionary.

Easier or More Difficult?

  • Easier: Lists of tuples allow for easy iteration and can be useful in contexts where order matters.
  • More Difficult: Searching for a specific developer or director would be less efficient since you’d have to iterate through the list rather than directly access the value via a key in a dictionary.

This completes the guidelines you provided! If you have further questions or need more assistance, feel free to ask!