Python Dictionaries

Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Dictionary is a python data type that is used to store key value pairs. It enables you to quickly retrieve, add, remove, modify, values using key. Dictionary is very similar to what we call associative array or hash on other languages.

Note: Dictionaries are mutable.

Creating Dictionary

Dictionaries can be created using pair of curly braces ( {}  ). Each item in the dictionary consist of key, followed by a colon, which is followed by value. And each item is separated using commas ( ,). Let’s take an example.

here friends  is a dictionary with two items. One point to note that key must be of hashable type, but value can be of any type. Each key in the dictionary must be unique.

Retrieving, modifying and adding elements in the dictionary

To get an item from dictionary, use the following syntax:

if the key exists in the dictionary, the value will be returned otherwise KeyError exception will be thrown.

To add or modify an item, use the following syntax:

Deleting Items from dictionary.

If the key is found then item will be deleted otherwise KeyError  exception will be thrown.

Looping items in the dictionary

You can use for loop to traverse elements in the dictionary.

Find the length of the dictionary

You can use len()  function to find the length of the dictionary.

in or not in operators

in  and not in  operators to check whether key exists in the dictionary.

Equality Tests in dictionary

==  and !=  operators tells whether dictionary contains same items not.

Note: You can’t use other relational operators like <  , > , >= , <=  to compare dictionaries.

Dictionary methods

Python provides you several built-in methods for working with dictionaries.

Methods Description
popitem() Returns randomly select item from dictionary and also remove the selected item.
clear() Delete everything from dictionary
keys() Return keys in dictionary as tuples
values() Return values in dictionary as tuples
get(key) Return value of key, if key is not found it returns None, instead on throwing KeyError exception
pop(key) Remove the item from the dictionary, if key is not found KeyError will be thrown


 

In next post we will learn about Python tuples.

Other Tutorials

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

4 Comments on "Python Dictionaries"

Notify of
avatar

Sort by:   newest | oldest | most voted
Hassaan
Guest
Hassaan
6 months 28 days ago

Everything is very well explained.Thankyou guys.
Keep it up

hamzah
Guest
hamzah
1 year 8 days ago

thank you for the clear explanation.

there is missing semicolon in ‘Looping items in the dictionary’ section, on example code line 6.

Sunghwan Park
Guest
Sunghwan Park
1 year 3 months ago

You guys’ lecture note is really nice and I would like to recommend to python new comers. Thanks 🙂

wpDiscuz