This video belongs to the openHPI course Python – schnell und intensiv Programmieren lernen. Do you want to see more?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:00Welcome to the last lesson this week. Dictionaries, also called dictionaries,
- 00:05but mostly called a dictionary in German as well. It stores data in the form of
- 00:13key-value. This is optimized to retrieve values very quickly when the key is known,
- 00:20and fast here means in terms of computing. If the dictionary is very large and contains a lot of
- 00:26things in it, it's still very fast. Keys are unique and the data type of the values
- 00:33doesn't matter. It could also be a list or another dictionary or whatever. A few
- 00:39usage examples. You would use this for a product catalog. Or in a hospital for
- 00:45patient IDs, if additional information is linked to the ID. Or HTTP status codes,
- 00:53the code and the meaning of that status. You can create a phone book or even
- 00:59an actual dictionary. Or in online shops, the shopping cart. They look like this. I have my
- 01:08object name and an equal sign to create the object. And then curly brackets.
- 01:13The entries are again separated by commas, as we also know from lists. Now
- 01:19we have a slightly different syntax. We have a key name, a colon
- 01:24and then the content. I have two examples here with a string and with an integer.
- 01:30And don't worry, that's not my real age, but I might like to be that old. I can
- 01:36use len() to see how many key-value pairs there are in this dictionary. Two pieces. I
- 01:44can access a tag with square brackets, as with lists. But now I'm not using
- 01:50the position. We had the third position or everything from the fourth to the end or
- 01:54or whatever. Now I take the name of the key. In this case, that's Name. And
- 02:00it's also case-sensitive. So if I did it with a capital N at the top,
- 02:06I have to do it that way now. I can also overwrite an entry if I want to change my age
- 02:11because it was my birthday, then I can do it that way. And from now on that's just
- 02:16changed. I can also add an entry, exactly the same syntax. If the key
- 02:22does not yet exist, it will be added. A few conventions for the names. I use these
- 02:30apostrophes, so a single quotation mark, in the dictionaries because I can work with them in f-strings
- 02:37well. I can't mix double and single quotation marks in the f-string
- 02:44We've already had an f-string. Here it is again as a reminder. f and then the
- 02:50quotation marks for the character string. And we can use the curly brackets here to
- 02:54do calculations or reference objects. And if I now want to access my dictionary
- 03:01I can do that with the quotation marks. I'll show you in the demo what it
- 03:06looks when you mix that. I've already prepared a bit here, a few words and
- 03:13then I can call it up and see that the value of A fits. It is 17. If I now also
- 03:22would work, but it doesn't work within
- 03:27the f-string and I get a syntax error because I'm working with double here. It would
- 03:33work again if I replace the double quotes with single ones, but since I usually use double quotes in the strings
- 03:38and therefore also do so in the f-string, I have simply got into the habit of
- 03:43to show the key name in the dictionaries with a single quotation mark from the outset and
- 03:54I use that consistently. I can access dictionaries with these square brackets,
- 04:02but I can also do it with another method that is error-proof. If I
- 04:07use the get method that is inherent in every dictionary, I can say, get the value for
- 04:17the key name and if the key is missing, then it returns a different value. In this case
- 04:23the key name is in there and so I get Berry. Now you may briefly consider what
- 04:27comes out if I write names in capital letters here and value if key is missing. That
- 04:35means that my program does not throw an error, but just does something alternative. I can have the
- 04:42keys, I can also output the keys once, it's more or less a list, it's not quite like
- 04:48but it's similar and I can do the same with the values, dozent.values and if
- 04:55I want to iterate, we'll do next week, then with items. I can see if the
- 05:03key has the entry age and that also works. I can see that this is true,
- 05:09but in large dictionaries I have the problem that the keys have to be calculated first and in
- 05:18the memory story have to be cached and then it is checked whether age is in there
- 05:23And I said yes, dictionaries are very fast at looking up data,
- 05:28so we can just do that, is there an age in the lecturer and that is both shorter in the code,
- 05:34and also faster. This speed only becomes important when you have large amounts of data,
- 05:38but that happens sometimes. I can also remove elements from a dictionary. I
- 05:45I inserted a new lecturer dictionary here and with del for delete I can
- 05:52delete a complete entry. It's just gone. I can also use pop()
- 05:58as with lists and here, too, I can specify which element is to be removed with
- 06:04the key name and that is also, as with pop, in the case of lists, the result or that,
- 06:11what is removed is returned. This means that I can save this in the “studie” object
- 06:18and then the lecturer no longer has the element in it. If I now try to remove the age
- 06:26we have already done that above, so the key is no longer there. I get
- 06:31an error. And here, too, I can avoid the error by saying, well, give a
- 06:36alternative back. For example, you could do non if I want to indicate, well, if it
- 06:42exists, then I'll delete it, but if not, then yes, that's okay too. Then
- 06:48just basically skip the rest. And if I want to remove all the entries, I can use clear.
- 06:54Then I see, Lecturer is now an empty dictionary. Again, a reminder that
- 07:01we have memory pointers, so pointers and we need copies, if we want to have copies.
- 07:06A dictionary 1 with a few things in it, a dictionary 2, which points to dictionary 1 and
- 07:13is now not an object itself, but just a pointer to the other, and Dict 3, which is an
- 07:21actual independent copy using the copy function, just like with lists. If I
- 07:26now change Dict 1, Dict 2 also changes, as with all other mutable objects.
- 07:33I change Dict 1, I write something different in the C entry and now I see that Dict 2 has also
- 07:38changed and dictation 3 is not. So, the same thing as we have already seen with lists, but it
- 07:44is important, that's why we repeated it again. Dictionaries have a great
- 07:49function, they can be updated if I have a dictionary with some things
- 07:53in it and a second one with maybe new values in it or new ones for the existing keys
- 08:00values. Here, for example, I see that I now increase the key A to 11 and add the key D
- 08:05I do that with Dict.Update. This updates the values that already
- 08:13for the existing keys and for new keys it adds them. That means that if I
- 08:17I now look at Dict 1, I can see that A is 11 and that D has been added. The
- 08:24others remain untouched. I can output a dictionary formatted and that is
- 08:33a great transition into next week. It's worth staying tuned to the course. Don't
- 08:38be surprised if we do a preliminary loop without explaining it in detail. It will all come next
- 08:41week. But we have a dictionary again and now I say I take these items and run
- 08:48a loop through them for the keys and values, hence K and V, and I now
- 08:54just a little bit in each iteration and then I can do a lot with very little code
- 08:59output. The entry has the value and I can format that and it's great. So
- 09:05just a sneak peek at some of the cool stuff we'll be doing next week.
- 09:09A recap at the end. So we saw that dictionaries are key-value pairs
- 09:17by indicating this with curly brackets and a colon or a colon. We
- 09:26have square brackets to indicate by name. Dictionaries are not ordered,
- 09:32so the key is not necessarily the first or second entry, but it is
- 09:38somewhere there. With get, I can get a replacement or a fallback if the
- 09:45key is not present and I can check if a key is present with in. I can
- 09:50remove things, there is also a replacement option there, and finally, as usual, dict.copy.
- 09:57Finally, the update, which is a great way to connect several data sources
- 10:04As always, have fun with the tasks, let me know if anything is unclear,
- 10:10that happens, it tends to be more our fault than yours, and we're happy to change it.
- 10:16And otherwise, have fun highlighting in your Revcard. Ciao, see you next week.
To enable the transcript, please select a language in the video player settings menu.