Skip to main content

Notes - Tuesday 12/22/20

Day 3 of Python Bootcamp LFG.

Dictionaries

Dictionaries are unordered mappings for storing objects. Mappings are collections of objects that are stored using a key. A sequence stores objects using the position. Mappings won't have an order and can't be sorted. They can't be indexed or sliced.

There are dictionaries that can be ordered called ordereddict which will be covered later.

Lists store objects in an ordered sequence. Dictionaries store objects using a key value pairing. The value is a Python object. This value pairing allows the user to grab the object without knowing the index location. Retrieving something from a dictionary can be faster than getting it from a list.

The syntax is:

```{'key1':'value1','key2':'value2'}````

Note the single quote ' is used.

To retreive something from the dictionary, you'd type in the dictionary name then pass the value you want. The syntax is:

urdictionary['urkeyhere']

Dictionaries can hold (among other things):

  • Lists
  • Floating point numbers
  • Strings
  • Integers
  • Other dictionaries

I can call dictionaries within dictionaries or list index positions within dictionaries. I would first retreive the object as seen above with the basic command urdictionary['urkeyhere'] but then I'd addon the second object or command I'm trying to pass. For example, if I wanted to retrive an index from the dictionary then call out index position 2, my command would look like:

urdictionary['urkeyhere'][2]

 

If I want to add to an existing dictionary, I would simply call the dictionary I previously defined, then add a new key. The syntax would be:

urdictionary['urnewkeyhere'] = urnewobjecthere

This command could be used to create a new object and key in the dictionary or modify an existing key with a new object.

 

Dictionaries have functions. Some examples are:

  • Keys - Shows all the keys in the dictionary
  • Values - Shows all the values aka objects in the dictionary
  • Items - Shows everything in the dictionary but as a tuple ("Too-pul") which will be discussed in a later topic.

The syntax would be: dictionary.function()

Remember like before, remember to include () or else you're asking Python to tell you what the function could do. Instead of actually executing the function.

Dictionary Manupulation Exercise

Let's do a basic example of manipulating a dictionary that has a list of letters

list = [a,b,c,d]

Let's say I want to retreive "c" then save it as a variable and capitalize it.

The long way looks like:

1 alphabet_dict = {"k1":['a','b','c','d']}
2 alphabet_dict
3 # Set up the dictionary and called it to confirm it's good.
4 alphabet_list = alphabet_dict['k1']
5 alphabet_list
6 # Set the variable alphabet_list to be the k1 key of the dictionary then called it to confirm it's good.
7 letter_c = alphabet_list[2]
8 letter_c
9 # Set the variable to pull "c" from the variable alphabet_list
10 letter_c.upper()
11 # Used function upper to capitalize "C"

Or you could stack calls on top of each other to make it easier. Python allows this flexibility. So easier:

1 alphabet_dict['k1'][2].upper()
2 # Did work from above lines in one step:
3 # First we re-use the alphabet_dict dictionary that was defined
4 # Second we call the key 'k1'
5 # Third we call index position 2
6 # Last we use function upper to capitalize that value