by BehindJava

What are Dictionaries in Python with Syntax and Examples

Home » python » What are Dictionaries in Python with Syntax and Examples

In this tutorial we are going to learn about Dictionaries in Python with few examples.

We’ve been learning about sequences in Python but now we’re going to learn about mappings in Python. If you’re familiar with other languages you can think of these Dictionaries as hash tables.

This section will serve as a brief introduction to dictionaries and consist of:

  1. Constructing a Dictionary
  2. Accessing objects from a dictionary
  3. Nesting Dictionaries
  4. Basic Dictionary Methods

So, What are mappings?

Mappings are a collection of objects that are stored by a key, unlike a sequence that stored objects by their relative position. This is an important distinction, since mappings won’t retain order since they have objects defined by a key.

A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.

Constructing a Dictionary

Let’s see how we can construct dictionaries to get a better understanding of how they work!

  1. Make a dictionary with {} and : to signify a key and a value

    my_dict = {'key1':'value1','key2':'value2'}
  2. Call values by their key

    my_dict['key2']
  3. Its important to note that dictionaries are very flexible in the data types they can hold. For

Example:

my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}

Accessing objects from a dictionary

Let’s call items from the dictionary.

my_dict['key3']
['item0', 'item1', 'item2']
  1. Can call an index on that value

    my_dict['key3'][0]
    'item0'
  2. Can then even call methods on that value.

    my_dict['key3'][0].upper()
    'ITEM0'
  3. We can also create keys by assignment. For instance if we started off with an empty dictionary, we could continually add to it.

    1. Create a new dictionary.

      d = {}
    2. Create a new key through assignment

      d['animal'] = 'Dog'
    3. Can do this with any object

      d['answer'] = 42

      Nesting with Dictionaries

      Hopefully you’re starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them.

Let’s see a dictionary nested inside a dictionary:

  1. Dictionary nested inside a dictionary nested inside a dictionary

    d = {'key1':{'nestkey':{'subnestkey':'value'}}}
  2. Keep calling the keys

    d['key1']['nestkey']['subnestkey']-‘value

    A few Dictionary Methods

    There are a few methods we can call on a dictionary.

Let’s get a quick introduction to a few of them:

  1. Create a typical dictionary.

    d = {'key1':1,'key2':2,'key3':3}
  2. Method to return a list of all keys .

    d.keys()
    dict_keys(['key1', 'key2', 'key3'])
  3. Method to grab all values.

    d.values()
    dict_values([1, 2, 3])
  4. Method to return tuples of all items (we’ll learn about tuples soon).

    d.items()
    dict_items([('key1', 1), ('key2', 2), ('key3', 3)])