Data Structures - Dictionary

Dictionary is a collection that holds items (of different types). Dictionary is ordered and changeable.

  • Mutable - the programmer can edit the dictionary after its creation - removing or adding items.
  • Ordered - the order of the items cann't change. Notice as of python 3.7 dictionaries are ordered. In earlier versions dictionaries are unordered.

Instead of "regular" numeric indexes, dictionary has keys and values. It is up to the programmer to decide how to pair the keys and the values. This is a very important feature of the dictionary - the programmer can choose to use intuitive keys (watch the examples).

A dictionary is created by curly brackets {}.
The pairs of keys and values are added using a colon.
The left side is a key and the right side is the value – {key1 : value1, key2 : value2}.

Here is an example:

#Creating a dictionary my_dict = {1 : "python", 3 : False, "songs" : ["Layla", "Born to Run", "Stairway to Heaven"], "bands" : ["Queen", "Radiohead"]} print(my_dict) type(my_dict)

Access Items

Accessing items is done by placing the relevant key inside square brackets next to the dictionary - dictionary[key].

#Dictionary my_dict = {1 : "python", 3 : False, "songs" : ["Layla", "Born to Run", "Stairway to Heaven"], "bands" : ["Queen", "Radiohead"]} print(my_dict["bands"]) print(my_dict["songs"]) print(my_dict[1]) print(my_dict["singers"])

In the last example above, we try to access a key that doesn't exist - an error is raised.

Add Items

Adding items is done by placing the key in square brackets next to the dictionary - dictionary[key] = value.
Editing items is done exactly the same.

#Dictionary my_dict = {1 : "python", 3 : False, "songs" : ["Layla", "Born to Run", "Stairway to Heaven"], "bands" : ["Queen", "Radiohead"]} #Add item my_dict["singers"] = ["Elvis Presley", "Whitney Houston", "Sam Cooke"] print(my_dict["singers"]) #Edit item my_dict["singers"] = ["Beyoncé"] print(my_dict["singers"])

Dictionary - Functions and Methods

Len

The len() function returns the length of a dictionary (the number of keys it has).

#len() our_dict = {"colors" : ["blue", "green", "gray", "orange"]} print(len(our_dict)) print(len(our_dict) / 2)

Pop

Using the pop() method we can remove an item (a key-vlaue pair) by using the appropriate key.
The method returns the items (watch example).

#pop() our_dict = {"colors" : ["blue", "green", "gray", "orange"]} item = our_dict.pop("colors") print(item) print(our_dict)

Keys

Using the keys() method we can check what keys the dictionary has.

#keys() our_dict = {"colors" : ["blue", "green", "gray", "orange"]} print(our_dict.keys()) print("Is the key 'colors' in our_dict? ", "colors" in our_dict.keys())

Exercise 1

Create a dictionary and access one of its items.
Try to use different data types!

#Create a dictionary #Access one of its items

Exercise 2

  1. Create an empty dictionary.
  2. Add as many items as you wish.
  3. Check if the key "python" is one of your dictionary's keys.
  4. Remove one of the items.
  5. Print one of the items.
  6. Print the length of the dictionary.

#Create an empty dictionary #Add items #Check if the key "python" is one of your dictionary's keys #Remove an item #Print item #Print length