Python Dictionaries

A Dictionary is a collection of many items. The indices of dictionaries can use different data types. Indices for dictionaries are called keys and values. Key with its associated value is called key:value pair. For example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}

Accessing Dictionary Items

You can access the dictionary items by referring to the key.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. a = dict1["fruit"]
  3. print(a)

Output:

apple

Dictionary have also a get() method that will give the same output.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. a = dict1.get("fruit")
  3. print(a)

Output:

apple

Changing Items

You can change the dictionary items by referring to the key.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. dict1["fruit"] = "banana"
  3. print(dict1)

Output:

{'fruit': 'banana', 'color': 'red', 'size': 'small'}

Adding Items

You can add a new item in a dictionary using a new key-value pair.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. dict1["price"] = "25"
  3. print(dict1)

Output:

{'fruit': 'apple', 'color': 'red', 'size': 'small', 'price': '25'}

Removing Items

You can remove a specified item by using the del statement.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. del dict1["size"]
  3. print(dict1)

Output:

{'fruit': 'apple', 'color': 'red'}

You can also use the del statement to delete the entire dictionary.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. del dict1
  3. print(dict1)

Output:

Traceback (most recent call last):
  File "C:\Users\Hp\Desktop\py\python.py", line 3, in <module>
    print(dict1)
NameError: name 'dict1' is not defined

Note: Since the total set is deleted an error is raised in the output.

The clear() statement will empty the whole dictionary.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. dict1.clear()
  3. print(dict1)

Output:

{}

Length of a Dictionary

You can define the length of a Dictionary using the len() method.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. print(len(dict1))

Output:

3

Checking Whether a Key Exists in a Dictionary

The in and not in operators can check whether a certain key exists in a dictionary.

Example:

  1. dict1 = {"fruit": "apple", "color": "red", "size": "small"}
  2. if "color" in dict1:
  3. print("Color is red")

Output:

Color is red

Dictionary Methods

There are a set of built-in methods in python which you can use on Dictionary.

Method Description
clear() Removes all values from a dictionary
copy() Returns a copy of a dictionary
fromkeys() Returns a dictionary which contains the specified keys and value
get() Returns the value of a specified key
items() Returns a dictionary which contains a tuple for each key value pair
key() Returns a dictionary which contains the dictionary's keys
pop() Removes a value with the specified key
setdefault() Returns the specified key's value. If the key does not exist adds the key with the specified value.
update() Updates the dictionary with specified key-value pairs
values() Returns a list of all values from the dictionary