avatarLiu Zuo Lin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

954

Abstract

hes and walk about with their brothers in the shade, doing nothing.</i></p><p id="0c98"><i>What I want should not be confused with total inactivity. Life is what it is about; I want no truck with death.</i></p><p id="a708"><i>If we were not so single-minded about keeping our lives moving, and for once could do nothing, perhaps a huge silence</i></p><p id="a475"><i>might interrupt this sadness of never understanding ourselves and of threatening ourselves with death.</i></p><p id="10ff"><i>Perhaps the earth can teach us as when everything seems dead and later proves to be alive.</i></p><p id="05d9"><i>Now I’ll count up to twelve and you keep quiet and I will go.</i></p><h2 id="fcce">The Reckoning.</h2><p id="c53c">Looking back, it has been gathering momentum, the mad rush to achieve what we want, at any cost:</p><p id="0f33">To reap the seas leaving plastic in our wake.</p><p id="e9e3">To demolish swathes of virgin forest, leaving desolation.<

Options

/p><p id="e2fa">To channel rivers where we want them to go, leaving ecological wastelands.</p><p id="373d">To wage wars for our own ends, leaving trails of broken people,</p><p id="887c">Children sobbing, mothers weeping, fathers forlorn,</p><p id="e174">A march to foreign borders, forsaking safety.</p><p id="8016">Where will it end?</p><p id="e9ee">And now, a pestilence rages across our world, leaving devastation in its path.</p><p id="a3f7"><i>But, giving us the opportunity to pause and reflect.</i></p><h2 id="0b41">It is Time:</h2><h2 id="e9fe">Nature and the universe agreed, it’s time to take stock.</h2><p id="7c23"><i>For once on the face of the earth, let’s not speak in any language; let’s stop for one second, and not move our arms so much.</i></p><p id="b99a"><i>Now I’ll count up to twelve and you keep quiet and I will go.</i></p><p id="255b">Lynette Clements. 2020. The simplicity of language, the urgency of need.</p></article></body>

Stop Using dict[key] to Access Values in Python Dictionaries!

# Reducing the chance of crashing your Python script

We probably learnt to use dict[key] to access a value in a dictionary at the start. But here’s a better way to do that.

The original dict[key] method

d = {'apple':4, 'orange':5, 'pear':6}

x = d['apple']    # 4
y = d['orange']   # 5
z = d['pear']     # 6

We use d[key] to access the value of key in a dictionary d.

a = d['pineapple']  # KeyError

However, if a key doesn’t exist in the dictionary, the d[key] raises a KeyError, which crashes our Python script. Which can get really annoying quickly.

A Sad True Story

So I was running a web scraping script a couple years ago.

Estimated time of completion: Overnight (many many pages)

So I went to sleep, hoping it’ll be done by tomorrow.

It crashed due to a KeyError

The KeyError was from some dumb unimportant part with dict[key]

I just wasted 8 hours of my life due to dict[key]

Using dict.get(key) instead!!

d = {'apple':4, 'orange':5, 'pear':6}

x = d.get('apple')      # 4
y = d.get('orange')     # 5
z = d.get('pear')       # 6
a = d.get('pineapple')  # None

When we use dict.get(key) instead of dict[key]:

  • they have the same behaviour if the key exists in the dictionary
  • dict[key] raises a KeyError if the key doesn’t exist. Code crashes.
  • dict.get(key) returns None if the key doesn’t exist. No crashes.

Or use dict.get(key, defaultValue)

Use if you want to set a default value instead of None

d = {'apple':4}

x = d.get('apple')          # 4
y = d.get('orange', 100)    # 100
z = d.get('pear', 3.14159)  # 3.14159

When we use dict.get(key, defaultValue)

  • if key exists, the actual value will be returned
  • if key doesn’t exist, defaultValue will be returned

Conclusion

Hopefully this was helpful!

Some Final words

If this story was helpful and you wish to show a little support, you could:

  1. Clap 50 times for this story (this really, really helps me out)
  2. Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)
  3. Leave a comment telling me what you think!

My Ebooks: https://zlliu.co/ebooks

My LinkedIn: https://www.linkedin.com/in/zlliu/

Python
Programming
Dictionary
Recommended from ReadMedium