From the course: Python Data Structures: Dictionaries

Creating a dictionary - Python Tutorial

From the course: Python Data Structures: Dictionaries

Start my 1-month free trial

Creating a dictionary

- [Instructor] Here is a brief example of using a dictionary of the Python interpreter prompt. At the command line prompt, type the word "python". In the following interactive session, the three greater than signs represent the Python interpreter prompts. The sal_info is the name of the dictionary I am using. sal_info is a dictionary that holds the names of all the major cities in the USA and the average salaries of the software developers as their values. So let's say I do sal_info equal braces, single quotes, Austin, colon, 91185, comma that separates one key from the other, close braces, Enter. Now, sal_info displays the contents of the dictionary. And this lists the list of cities and the respective average salaries of their software developers. To just access the salary of Boston, say, we can just call the key within square brackets. So sal_info['Boston'] equal, and we can reset the value to 95123. So now when we do sal_info, we notice that Boston has the new value assigned to it. Let's say we want to add a new element. So we say sal_info and add a new key of a new city, say Atlanta. And we assign a new value to it, 91234. Now when we do sal_info, again, it prints the contents of the dictionary including Atlanta. Notice that the keys and the values are displayed in the order they were inserted. They're all not sorted. If you remember from our previous video, the key is displayed in the order it was inserted. In this case, Atlanta shows up as the last one. Let's say I want to find the length of my dictionary. I do sal_info. It shows up as three. If I want to delete one element, I do del, name of dictionary, sal_info, square brackets, Atlanta. Now, when I see the contents again, I notice that Atlanta no longer exists. If I do len of sal_info, I see that the length has been reduced by 1. Let's say I tried to access an element that's not in the dictionary. So say I do sal_info, square brackets, Atlanta. It gives me a key error. This is Python dictionaries way of giving an error, saying, that particular element does not exist in the dictionary. Finally, let's say you want to flush out the dictionary, that is, clean about and make it empty. Python gives you that ability to do that using the clear method. So sal_info.clear(). Now, when I look at the contents by doing sal_info, I see it's an empty dictionary. If I do length of sal_info, I see that the length is zero. Notice the syntax. Calling a method such as clear with parentheses, differs from calling a function such as length with parentheses. In clear, it's a method and a dictionary name such as sal_info and the method are connected using the dot operator, where the dictionary name comes first. In a function on the other hand, len calls the dictionary in parentheses as a parameter. Just like in lists, dictionaries have both functions and methods making this data structure highly versatile.

Contents