Python List and Dictionary Comprehensions with Morse Code
Channel: Dave Briccetti
Date: 2017-12-17

In order to cleanly “encode” Morse Code in a Python program, I show how to use a multiline string along with strip and split and list and dictionary comprehensions to build a dictionary. I visualize the code in the PythonTutor.com visualizer.

Please see this Twitter thread for some improvements: https://twitter.com/dcbriccetti/status/942198364069814273

After publishing this I discovered this fails on the space character (since it gets strip()’d), so now:
z --..
  --..--     # This is the space
? ..--..
! -.-.--
""".split("\n")
morse_codes = {el[0]: el[2:] for el in codes if el}