How To Use Dictionaries in Python

Dictionaries in Python
(Image credit: Tom's Hardware)

In Python, dictionaries are data storage objects that use a key to retrieve a value. Think of your cell phone contact list or phone book. We look for the name of the person, the key, and their phone number is the value. Dictionaries are incredibly useful when storing and sorting data. We used a dictionary in our for loop project which saw RSS news feeds used to generate content on a web page. 

We’re going to go through how to create, update and delete keys and values inside of a dictionary and then use a dictionary in a real world project where we create a notification system using Python and nfty.sh.

Latest Videos FromTom's Hardware
Les Pounder
Associate Editor

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • BillyBuerger
    Funny that this article showed up today as I just had to lookup python dictionaries this morning. Only because I only use python sparingly and I always forget little details of how it does some of its things compared to C#.
    Reply
  • bit_user
    I'm sure this is useful for some, but I hope every budding Python programmer has seen & knows how to find the Tutorial and Library Reference:
    https://docs.python.org/3/tutorial/datastructures.html#dictionarieshttps://docs.python.org/3/library/stdtypes.html#mapping-types-dict
    What would've made it useful for me is to know a bit about how Dictionaries are implemented and their performance-scaling characteristics. I assume they use some form of hash table, although I wouldn't mind knowing more specifics. Anyway, the article is long enough as is, but you might consider doing a "Python Code Optimization" tutorial, at some point.

    Another thought I just had is that you could point out interesting places & ways that Dictionaries play into the language, itself.
    Reply