March 18, 2021•295 words
Learning to code using Python
The last week or so has been great. It feels like progress.
Tutorial hell, purgatory, paralysis... About 14 months of it. It's not been bad at all. I have enjoyed it.
I quickly learned that tutorial videos are a waste of time for me. I zone out and day dream. Written tutorials, coding exercises, blog post, podcasts, newsletters, coding playlists, books and Slack channels have though been of benefit.
Python : sets and dict(zip()
dict(zip(iterable1, iterable2)) function is great. Take a couple of iterables (same length) and with just one line of code turn them into a dictionary. Other data structures can also be zipped but not yet had much call for them yet. The time will come and when it does I'll be ready!
Discovered something useful you can do with sets.
display = 'abracadabra' letter_count1 = [(display.count(letter)) for letter in set(display)] letter_count2 = [(display.count(letter)) for letter in display] print(letter_count1) [5, 2, 1, 1, 2] # using a set getting the frequency count of each letter a, b, c, d, r in the word. Duplicates are excluded. print(letter_count2) [5, 2, 2, 5, 1, 5, 1, 5, 2, 2, 5] # using a list getting the frequency count of each letter in the word as they appear in the word. Duplicates are not excluded.
The allowed the creation of a dictionary with key, value pairs of letter and frequency count using the
dict(zip()) function .
word = 'abracadabra' letter_count_list = [word.count(letter) for letter in word] letter_count = [(word.count(letter)) for letter in set(word)] # using set counts but hides duplicates! dictionary = dict(zip(set(word), letter_count)) for k, v in dictionary.items(): print(k, v) d 1 c 1 a 5 r 2 b 2