This is the third installment in the Learn Python with Pj! series. Make sure to read Part 1 – Getting started, Part 2 – Lists and loops, and Part 3 – Funcitons and strings. I’ve learned a lot with Python so far, but when I learned dictionaries (sometimes shortened to dicts), I was really excited about what could be done. A dictionary in Python is a series of keys and values stored inside a single object. This is kind of like a super array; one that allows you to connect keys and values together in a single easily accessible source. Creating dictionaries from arrays can actually be very simple, too. In this blog, I’ll dig into how to create dictionaries and how to read and write files in the code. Dictionaries Dictionaries in Python are indicated by using curly braces, or as I like to call them, mustaches. { } indicates that the list you’re looking at isn’t a list at all, but a dictionary. shows_and _characters = { “Bojack Horseman”: “Todd”, “My Hero Academia”: “Midoriya” “Ozark”: “Ruth” “Arrested Development”: “Tobias”, “Derry Girls”: “Sister Michael”, “Tuca & Bertie”: “Bertie” } This is a dictionary of my favorite TV shows and my favorite characters in that show. In this example, the key is on the left and the value is on the right. To access dictionaries, you use a similar call like you would for a list, except instead of an element number, you would put the key. print(shows_and_characters[“Ozark”]) would print Ruth to the console. Additionally, both the key and value in this example are strings, but that’s not a requirement. Keys can be any immutable type, like strings, ints, floats, and tuples. Values don’t have this same restriction, therefore values can be a nested dictionary or a list, in addition to the types mentioned for keys. For instance, the following dictionary is a valid dictionary. shows_with_lists = { “Bojack Horseman”: [“Todd”, “Princess Carolyn”, “Judah”, “Diane”], “My Hero Academia”: [“Midoriya”, “Shoto”, “All Might”, “Bakugo”, “Kirishima”], “Ozark”: [“Ruth”, “Jonah”, “Wyatt”], “Arrested Development”: [“Tobias”, “Gob”, “Anne”, “Maeby”], “Derry Girls”: [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”], “Tuca & Bertie”: [“Bertie”, “Speckle”, “Tuca”, “Dakota”] } In this example, each value is a list. So if we tried to print the value for the key ”Derry Girls”, we would see [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”] printed to the console. However, if we wanted the last element in the value list, we’d write shows_with_lists[“Derry Girls”] [-1]. This would print the last element in the list, which in this case is James. Dictionaries can be written manually, or, if you have two lists, you can combine the dict() and zip() methods to make the lists into a dictionary. list_of_shows = [“Bojack Horseman”, “My Hero Academia”, “Ozark”, “Arrested Development”, “Derry Girls”, “Tuca & Bertie”] list_of_characters = [[“Todd”, “Princess Carolyn”, “Judah”, “Diane”], [“Midoriya”, “Shoto”, “All Might”, “Bakugo”, “Kirishima”], [“Ruth”, “Jonah”, “Wyatt”], [“Tobias”, “Gob”, “Anne”, “Maeby”], [“Sister Michael”, “Orla”, “Erin”, “Claire”, “James”], [“Bertie”, “Speckle”, “Tuca”, “Dakota”]] combined_shows_characters = dict(zip(list_of_shows, list_of_characters)) print(combined_shows_characters) This is one way to create a dictionary. Another is called Dictionary Comprehension. This one is a little more work, but can be used in a variety of different ways, including using a bit of logic on a single list to generate a dictionary using that original list. Here’s […]