Skip to main content

Dictionary

Dictionary is a collection of key-value pairs.

Key is a string, value is the data assigned to the key (like a dictionary).

Dictionary is very similar to arrays but it has keys instead of indexes.

Declaring dictionary

Let's look at many examples.

  1. The key can be either a numebr or a string.
  2. The value can be any data type.

keys()

This method is used to get all the keys in the dictionary.

It is uesful when you want to iterate over the keys.

It returns dict_keys([keys]) data type and using list() method we can convert it to a list.

values()

This method is used to get all the values in the dictionary.

It is uesful when you want to iterate over the values.

It returns dict_values([values]) data type and using list() method we can convert it to a list.