Python Dictionaries

(Sponsors) 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 a key. Dictionary is very similar to what we call associative array or hash on other languages.

Note: Dictionaries are mutable.

Creating a Dictionary

Dictionaries can be created using a pair of curly braces ( {}  ). Each item in the dictionary consists of a key, followed by a colon, which is followed by a 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 a hashable type, but the 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 the 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 the dictionary.

If the key is found the 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 the same items not.

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

Dictionary methods

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

Methods Description
popitem() Returns randomly selected item from the dictionary and also remove the selected item.
clear() Delete everything from a dictionary
keys() Return keys in the 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 of throwing KeyError exception
pop(key) Remove the item from the dictionary, if the key is not found KeyError will be thrown

In the next post, we will learn about Python tuples.

Other Tutorials (Sponsors)

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!

6 thoughts on “Python Dictionaries

  1. Sunghwan Park

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

  2. hamzah

    thank you for the clear explanation.

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

  3. Amir

    Hello again,
    Returns randomly **select** item from dictionary and also remove the selected item.

    select = selected


Leave a Reply

Your email address will not be published. Required fields are marked *