Notes and experiences for yesterday and today.
I ‘wasted’ a lot of time, again, struggling with exercises yesterday and ended up being too zombied to even write the summary post.
Have been much smarter about it today.
Stopped after about 30 mins and copied and tried to understand the solution.

Notes

List of Dictionaries

  • Just more convenient than a list of lists, not particularly faster
  • Make it more flexible.

Dictionary of dictionaries

  • Think beforehand of what your keys are going to be.
  • Looking in them, faster than lists
  • The tradeoff is more memory usage

Dictionary of Lists

  • Works when you need disparate elements to work with later. Those can be stored as dictionary values, in a list form.

Part 3

Decimals

  • To have something sane between integers and crazy floats.
  • I import the Decimal class and then create objects, like so
from decimal import Decimal
a = Decimal('0.1')
b = Decimal('0.2')
  • I create them from strings not floats, just like I would int strings.
  • It has various methods I can call on the Decimal object.
    • Stuff like getcontext with has a method to set the precision of units after the decimal point.
  • Helpful if you need precision at a certain level.

Named Tuples

  • A very handy way of calling values in a tuple by sensible names, just like dictionaries
  • Very handy instead of heavyweight dictionaries.
  • Create them like so
from collections import namedtuple  
Person = namedtuple('Person', ['first', 'last', 'age'])  
me = Person('Jason', 'Braganza', 42)  
  • Doing a me.first will get me Jason, just like a dictionary.
  • I can still refer to the tuples with the numeric indices.
  • I can do a ._replace to replace values, if I want to. me._replace(first ='Mario'). It just returns a new object with the same name, which I can choose to catch in a new variable.
  • Better for readability.
  • Basic uses. The moment I want to twiddle with it too much, I know I should just make my own classes

defaultdict

  • A variation/improvement on the normal dictionary.
  • We want a dictionary, with default values.
  • When I check for the presence of a key, if it does not exist it’ll get created with a default value.
  • We give it a function or a class, that will run, be called everytime a key does not exist.
  • So I could pass it basic primitives, like a list with no entries and it will return an empty list for a value everytime, there is not one. Or saying int, in which case it will create a key with 0 for a value
  • Create them like so
from collections import defaultdict  
d = defaultdict(int)  
  • So now when I do d['a'], it well check for key a. If it does not exist, it will create it, and give it a value of 0 by running the int function with no arguments.
  • I can use any function or callable, as long as it has no arguments.

OrderedDict

  • No need to use it anymore
  • But handy in case I am looking at old code.
  • Regular dictionaries order stuff chronolgically as of Python 3.7.
  • Which is what ordered dictionaries used to do.

Counter

  • from collections import Counter
  • if I pass an Counter dictionary an iterable, it will create a dictionary with the occurrence of each element.
  • if I go c = Counter[10, 10, 20, 50, 60, 60, 80, 80, 80, 90'], then I get a dictionary like Counter({10: 2, 20: 1, 50: 1, 60: 2, 80: 3, 90: 1})
  • I can also do a most common method to get the most frequently used elements. c.most_common()

Enums

  • from enum import Enum
  • Create ’em like
class ColorEnum(Enum)
Red = 1
Blue = 2
Green = 3
  • Matches symbolic labels / names to numeric values.
  • If a = ColorEnum.Red and b=ColorEnum.Red then they would both be equal.

Learning / Feedback/ Experiences from doing exercises

  • Reuven keeps harping on the fact, that I need to think things through.
    • Is this how you want to build things?
    • What are the ramifications?
    • What if you want to change up things or if things are expected to grow exponentially in the future?
    • Think! Be itentional. Design accordingly.
  • Caught myself frequently initalising a total in a loop and then wondering why I would not get the actual total :)
  • Now my approach, kinda matches what Reuven does. Which means, I am beginning to write some sane code :)
  • Finished with this course. And on to the next one. Planning, building in margin and working at it a little bit everyday helping a lot!

Read all about my Python advanced data structures journey here.