From Lists to Dictionaries
Hello, welcome back to Python for the Humanities. Today we are talking about dictionaries.
Revisiting Lists: Accessing Specific Items
First, to begin with, let’s see how we can access lists in a different way. We have, I think, used it already, but perhaps I have not explained it fully.
A for loop is perfect when you want to do something with every item in the list, but what do you do if you need only a specific item? For example, you want only the first author in a list, or you want only the last event. You do this by using an index—an address within the list. You put this index into square brackets.
The crucial thing to remember is that Python always starts counting from 0, not 1. The first element always has an index of 0, the second is at the index 1, and so on. Here you see an example: we have a list of art movements, and then we want to access the very first item.
art_movements = ["Renaissance", "Baroque", "Neoclassicism", "Romanticism"]
# Access the first item
first_movement = art_movements[0]
print(first_movement) # Output: RenaissanceI have to use the index 0, because otherwise I will get the wrong one. So, don’t start with one. The third movement, therefore, would be index 2, and so on. Always take an index less than the number of the movement that you want.
The Negative Indexing Trick
Now, for the very last movement, there’s another trick you can use: -1. When you start with negative counting (but not -0, as -0 is the same as 0), you count from the end. So, -1 is the first element from the end (the last element), and then -2 is the second from the end, and so on.
# Access the last item
last_movement = art_movements[-1]
print(last_movement) # Output: Romanticism
# Access the second to last item
second_last_movement = art_movements[-2]
print(second_last_movement) # Output: Neoclassicism
You use an index when the position of the item is what matters to you—when you want to get the very first element, the last element, or to compare two specific items. We had this in an exercise where we were trying to get the initials of a person, where we found where the space is between the different parts of a name and then we used the first letter that comes after the space for the initial. This is a typical example where you want to address a particular letter within a string.
Lists are not just a bag of items, but an ordered sequence. And a string is not just a monolithic string, but it’s again a list; it’s an ordered sequence of characters.
Introducing Dictionaries: Python’s Index Cards
Now, a different form of collection is a dictionary. Like a list, a dictionary is a collection, meaning a data type that collects and manages many elements. But where a list addresses the items with an index, a dictionary can address more complex items that are more like an index card. The whole index card is one element, and this has different parts to it.
Let’s say for a person, you want to manage people with an index card. You have an index card, and it has the name and the surname and the age and the occupation of this person. You have 4 elements, and you cannot do this with a list, because the list is a flat list of things of the same type. A dictionary can do that.
A Python dictionary stores data by associating a unique key with a value. You use curly brackets or curly braces ({}), and each entry is a key-value pair. You have a key and a value, and they’re separated by a colon.
Look at this example here. We have book metadata, and every book has a title. You say title is a string (it still needs to be in quotes because it’s a string, not something Python knows about; you can use whatever you want), and then you say the title is, colon (“:”), “Their Eyes Were Watching God.” You have an author, the author is, and then you have the name of the author. You have a year of publication, and then you have a number that’s the year of publication. And you have genres, and then you have a list of genres.
You see how this whole book_metadata is like one index card about one book. It has these curly brackets to delimit it, and within it, every field has a key like title and a value, “Their Eyes Were Watching God.” You can have as many key-value pairs as you like, and they are all separated with a comma from each other.
book_metadata = {
"title": "Their Eyes Were Watching God",
"author": "Zora Neale Hurston",
"year": 1937,
"genres": ["Fiction", "Modernism", "Folk novel"]
}Accessing Dictionary Values
You access a value by providing its key in square brackets. In this way, it’s similar to a list.
# Pull out the value from the key “author”
author_name = book_metadata["author"]
print(author_name) # Output: Zora Neale Hurston
This whole book_metadata is one element, one index card, let’s say, for one book—one library card.
Handling Multiple Records: A List of Dictionaries
Now, if I have many books, I would use a list like we already know. I would make a list of dictionary entries, and then I can have as many books as I want in my list.
library = [
{
"title": "Their Eyes Were Watching God",
"author": "Zora Neale Hurston",
"year": 1937
},
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"year": 1925
}
]
That’s all for this video. I just wanted to introduce the dictionary. Now we continue with the next video, where we will talk about other standard modules that are useful in Python and we have not yet talked about.
Thank you, and see you there.


