Thursday 18th March 2021

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()

The Python 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]

[5, 2, 1, 1, 2]  # using a set getting the frequency count of each letter a[5], b[2], c[1],  d[1], r[2]  in the word. Duplicates are excluded.

[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

You'll only receive email when they publish something new.

More from Alex Turner