Python Dictionaries
What are Python Dictionaries :
Dictionaries are Python’s implementation of a data structure that could also be known as an array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
Syntax :
Dictionaries are declared by using curly braces and a colon … like this
{ key : value }
The colon shows the separation between the key and the value. The Keys and Values stored in the dictionary can be a string, list, tuple, another dictionary, a float, or a number.
Accessing elements in a dictionary :
Let’s take the following as our example of a dictionary
my_dict={1 : “hello”, 2 : “bye”}
To access “hello” you have to specify the key which it is stored in. The current key is 1 so you would do something like this…
print(my_dict[1])
This line of code is accessing key 1 if you run this code your output will be…
“hello”
In this example I specified 1 so I will get an output of “hello” if you specify 2 then your output will be “bye”
Changing elements in a dictionary :
We will use the same dictionary as our example.
my_dict={1 : “hello”, 2 : “bye”}
To change hello to hi, you can one again specify the key and then change the value.
(my_dict [1] ) = “hi”
This will change what is stored in the dictionary but it will not give you an output. To see what is changed you have to print the dictionary.
print(my_dict)
Your output will be…
{1 : “hi”, 2 : “bye”}
Adding Elements into a Dictionary :
If the key is already present, then the existing value gets updated. In case the key is not present, a new key: value pair is added to the dictionary.
my_dict={1 : “hello”, 2 : “bye”}
To add another key:value pair you can do the following…
my_dict [3] = “see you”print(my_dict)
The output will be…
my_dict={1 : “hello”, 2 : “bye”, 3 : “see you”}
Removing Elements from a Dictionary :
- We can remove a particular item in a dictionary by using the pop() method
- All the items can be removed at once, using the clear() method
- We can also use the del keyword to remove individual items or the entire dictionary itself
1: pop()
This method removes an item with the provided key and returns the value
This is the dictionary example…
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
To “pop out” the number 16, access key 4
print(squares.pop(4))
The output will be 16 but if you print the squares it will look something like this…
{1:1, 2:4, 3:9, 5:25}
2:clear()
Let’s say you want to remove the whole squares dictionary. You could use clear to do that.
squares.clear()print(squares)
The output will be…
{ }
You are getting an empty dictionary because everything is cleared out.
3:del[]
Ok, let’s bring back the squares variable and redefine it.
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
To delete the third key you can use the del function. The del function does not print the key that is deleted but instead just deletes it.
del squares[3]
This line of code will delete key 3, when you print the dictionary your end result will be the following…
{1: 1, 2: 4, 4: 16, 5: 25}
Python Dictionary Methods :
1: get()
The get() method returns the value of the item with the specified key.
dictionary.get(keyname, value)
Parameter Values
This is going to be our dictionary example
tech={"brand": "apple", "model": "iphone 12", "year": 2021}
Using the get function
x = apple.get(“price”, 15000)
print(x)
This output will be…
15000
Let’s see what will happen if you print tech
print(tech)
The output is…
{"brand": "apple", "model": "iphone 12", "year": 2021}
Nothing changed. The get function is not permanent you can use it and get an output, but when you print the tech variable it will stay the same.
2: items()
The items() method returns a view object. The view object contains the key-value pairs of the dictionary, as tuples in a list.
dictionary.items()
We will use the same dictionary
tech={"brand": "apple", "model": "iphone 12", "year": 2021}
Let’s make a variable to store the items method in
x=tech.items()
print(x)
This output will show you everything that is stored in the dictionary in groups
dict_items([('brand', 'apple'), ('model', 'iphone 12'), ('year', 2021)])
3: keys()
The keys() method returns a view object. The view object contains the keys of the dictionary, as a list.
dictionary.keys()
Once again, we are going to use the same dictionary
tech={"brand": "apple", "model": "iphone 12", "year": 2021}
x=tech.keys()
This will print all the keys in tech
dict_keys(['brand', 'model', 'year'])