1. Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.
Each dictionary is a set of key: value pairs. You can create it by using the following syntax:
1 2 3 4 5 |
my_dictionary = { key1: value1, key2: value2, key3: value3, } |
2. If you want to access a dictionary item, you can do so by making a reference to its key inside a pair of square brackets (ex. 1) or by using the
get()
method (ex. 2):
1 2 3 4 5 6 7 8 9 10 11 |
pol_eng_dictionary = { "kwiat": "flower", "woda": "water", "gleba": "soil" } item_1 = pol_eng_dictionary["gleba"] # ex. 1 print(item_1) # outputs: soil item_2 = pol_eng_dictionary.get("woda") print(item_2) # outputs: water |
1 2 3 4 5 6 7 8 9 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } pol_eng_dictionary["zamek"] = "lock" item = pol_eng_dictionary["zamek"] print(item) # outputs: lock |
4. To add or remove a key (and the associated value), use the following syntax:
1 2 3 4 5 6 7 |
phonebook = {} # an empty dictionary phonebook["Adam"] = 3456783958 # create/add a key-value pair print(phonebook) # outputs: {'Adam': 3456783958} del phonebook["Adam"] print(phonebook) # outputs: {} |
You can also insert an item to a dictionary by using the
update()
method, and remove the last element by using the popitem()
method, e.g.:
1 2 3 4 5 6 7 |
pol_eng_dictionary = {"kwiat": "flower"} pol_eng_dictionary.update({"gleba": "soil"}) print(pol_eng_dictionary) # outputs: {'kwiat': 'flower', 'gleba': 'soil'} pol_eng_dictionary.popitem() print(pol_eng_dictionary) # outputs: {'kwiat': 'flower'} |
5. You can use the for
loop to loop through a dictionary, e.g.:
1 2 3 4 5 6 7 8 9 10 11 12 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } for item in pol_eng_dictionary: print(item) # outputs: zamek # woda # gleba |
6. If you want to loop through a dictionary’s keys and values, you can use the items()
method, e.g.:
1 2 3 4 5 6 7 8 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } for key, value in pol_eng_dictionary.items(): print("Pol/Eng ->", key, ":", value) |
Output:
1 2 3 |
Pol/Eng -> zamek : castle Pol/Eng -> woda : water Pol/Eng -> gleba : soil |
or
1 2 3 4 5 6 7 8 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } for value in pol_eng_dictionary.items(): print(value) |
Output:
1 2 3 |
('zamek', 'castle') ('woda', 'water') ('gleba', 'soil') |
7. To check if a given key exists in a dictionary, you can use the in
keyword:
1 2 3 4 5 6 7 8 9 10 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } if "zamek" in pol_eng_dictionary: print("Yes") else: print("No") |
8. You can use the del
keyword to remove a specific item, or delete a dictionary. To remove all the dictionary’s items, you need to use the clear()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } print(len(pol_eng_dictionary)) # outputs: 3 del pol_eng_dictionary["zamek"] # remove an item print(len(pol_eng_dictionary)) # outputs: 2 pol_eng_dictionary.clear() # removes all the items print(len(pol_eng_dictionary)) # outputs: 0 del pol_eng_dictionary # removes the dictionary |
9. To copy a dictionary, use the copy()
method:
1 2 3 4 5 6 7 |
pol_eng_dictionary = { "zamek": "castle", "woda": "water", "gleba": "soil" } copy_dictionary = pol_eng_dictionary.copy() |
Exercise 4
Write a program that will “glue” the two dictionaries (d1
and d2
) together and create a new one (d3
).
1 2 3 4 5 6 |
d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'} d2 = {'Mary Louis': 'A', 'Patrick White': 'C'} d3 = {} for item in (d1, d2): # Write your code here. print(d3) |
Sample solution:
1 2 3 4 5 6 7 8 |
d1 = {'Adam Smith': 'A', 'Judy Paxton': 'B+'} d2 = {'Mary Louis': 'A', 'Patrick White': 'C'} d3 = {} for item in (d1, d2): d3.update(item) print(d3) |
Exercise 5
Write a program that will convert the my_list
list to a tuple.
1 2 3 4 |
my_list = ["car", "Ford", "flower", "Tulip"] t = # Write your code here. print(t) |
Sample solution:
1 2 3 4 |
my_list = ["car", "Ford", "flower", "Tulip"] t = tuple(my_list) print(t) |
Exercise 6
Write a program that will convert the colors
tuple to a dictionary.
1 2 3 4 |
colors = (("green", "#008000"), ("blue", "#0000FF")) # Write your code here. print(colors_dictionary) |
Sample solution:
1 2 3 4 |
colors = (("green", "#008000"), ("blue", "#0000FF")) colors_dictionary = dict(colors) print(colors_dictionary) |
Exercise 7
What will happen when you run the following code?
1 2 3 4 5 |
my_dictionary = {"A": 1, "B": 2} copy_my_dictionary = my_dictionary.copy() my_dictionary.clear() print(copy_my_dictionary) |
The program will print {'A': 1, 'B': 2}
to the screen.
Exercise 8
What is the output of the following program?
1 2 3 4 5 6 7 8 9 |
colors = { "white": (255, 255, 255), "grey": (128, 128, 128), "red": (255, 0, 0), "green": (0, 128, 0) } for col, rgb in colors.items(): print(col, ":", rgb) |
white : (255, 255, 255)
grey : (128, 128, 128)
red : (255, 0, 0)
green : (0, 128, 0)
Ex. 9
What is the output of the following code snippet?
1 2 |
d = {'c': 3, 'a': 1, 'a': 5, 'a': 2} print(d['a']) |
Which of the following statements correctly creates a list?
(Select two answers)
Given the code snippet below, what statement should you insert in place of the comment line to get the following output:
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
?
1 2 3 4 5 6 |
d1 = {1: 'one', 2: 'two'} d2 = {3: 'three', 4: 'four'} # Insert code here print(d1) |
Ex. 12
Which of the following dictionaries are created correctly?
(Select two answers)
Ex. 13
What is the output of the following code snippet?
1 2 |
d = {3: 'a', 5: 'b', 1: 'c', 7: 'd'} print(sorted(d)) |
Ex. 14
Consider the following dictionary:
1 |
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}. |
Which of the following statements removes the element with the key 'c'
from the d
dictionary?
(Select two answers)