Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Operators and Methods for Dictionaries in Python

Give Feedback

Dictionaries have some of the same operators and built-in functions that can be used with strings, lists, and tuples. For example, the in and not in operators return True or False according to whether the specified operand occurs as a key in the dictionary.

You can use the in operator together with short-circuit evaluation to avoid raising an error when trying to access a key that is not in the dictionary. The len() function returns the number of key-value pairs in a dictionary.

Now that you’ve completed this course, you can test your knowledge with this quiz:

Wiggers on July 30, 2019

Very well presented and shows I am not the only one who can’t type and spell!

ALXTheMaster on July 31, 2019

Great course!

leesmith4044 on Aug. 2, 2019

What method[s] do you suggest for creating a large dict from a file?

herma48852 on Aug. 2, 2019

Excellent presentation!

Anonymous on Aug. 3, 2019

Cool thanks..

Sergio on Aug. 4, 2019

Would be nice to have included methods to mass load a dictionary (e.g. from a JSON file) or save dictionary values… Nevertheless, it was fine! Thanks!

justchris86 on Aug. 7, 2019

Love it! this is basically an API call isn’t it?

muralichintapalli on Aug. 10, 2019

It would be really helpful to have real world example of Dict and also to build dictionaries from some file

Daniel on Aug. 13, 2019

Great course.

muthu raja on Aug. 14, 2019

Reading/Importing JSON data as dicitionary.

import json

jsonData = '[{"FirstName" : "Bob", "LastName" : "Johnson"}, {"FirstName": "Andrew", "LastName" : "Brown"}]'

# if you want to parse JSON file, use json.load() function instead
parsedJson = (json.loads(jsonData)) 

"""
parsedJson is stored as list
# whereas, each item within parsedJson list is stored as dictionary
"""
print(parsedJson)
print(type(parsedJson)) 
#print(type(parsedJson[0])) 

#Reading values from list, and then fetch FirstName, LastName from dictionaries
for x in parsedJson:
    print(x["FirstName"], x["LastName"])

Dibsdalec on Aug. 20, 2019

The get method has None as default, this default can be changed.

Would be nice to discuss collections, DefaultDict, in Collections used to avoid key errors if you try and query a key that does not exist.

Jean Ferreira on Sept. 1, 2019

Thanks for the videos.

Pygator on Sept. 14, 2019

Excellent coverage of the basics of Dicts in Python. An advanced course with projects using them would be equally great.

sideproject8 on Dec. 17, 2019

Out of all the video instructors so far I enjoy your style the best. Calm, chill, takes time to explain things, and enjoyable to listen to. Props you to my man, I hope more comes from you.

JulianV on Dec. 19, 2019

Thanks Paul. Nice!

Paulo Szewierenko on March 16, 2020

Objective and direct! Very useful!

sweir12525 on June 22, 2020

You raced over restrictions so fast I had not idea what you meant.

Konstantin Schukin on July 2, 2020

Thank you for great course!

Ben Nduwuba on July 2, 2020

Superb course , thanks Paul

paolotagliente on July 27, 2020

thanks Paul.....enough to make me going nicely....this is a great base really.....

Ghani on Oct. 19, 2020

A very informative course; thank you so much!

Become a Member to join the conversation.