CodeCachet

Not Webster and Python Dictionaries - Python meetup #7

Never enough to say about lists - so before getting to the dict, we covered two examples using lists. The first one generates random passwords for you. We realized that it is easy to generate a random password, and a bit harder to crack - that we will do in a subsequent meeting, which should be interesting. Of course, the longer the password, and longer the character set you use, the harder it is to crack. The other program was going to be homework assignment, but we covered it anyway. The problem was to shuffle a list in place, just using random.randint() (and not random.shuffle()!). The result is a very simple program (using a take on the basic Fisher-Yates algorithm from 1938), but takes a bit to figure it out.

On to the famous Python dict! We learned:

  • how to initialize a dict by various means, including "statically", and by using the dict() function.
  • that immutibility (hashibility) of the key is key - but that usually, keys are strings.
  • putting a bunch of dict's into a list, and searching through, pulling out desired dict's as we go, as an example pattern that we might use, or find in the real world.
  • using a dict to store "tagged" data, which we can analyze and modify. This is a common use case.
  • using dict.get() to save us from undefined key exceptions and to set defaults, which can be handy.

But we didn't get to two exciting examples: counting, and grouping. That's for next time!

Check out this notebook for our dict notes and examples.