What is a Dictionary in Python and why do we need it

What is a Dictionary in Python and why do we need it

Dictionaries are Python’s implementation of an associative list, which may be a arrangement . A dictionary may be a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.

Dictionary in Python and why do we need it

1)Dictionary in Python

This is a dictionary example that contains cricket names as keys and the highest score as values. Elements are stored as key-value pairs in the dictionary, where each value is mapped to one key. It is also called a hash table or an associative array.

dictionary

There are four elements in the above dictionary, i.e. four key-value pairs.

  1. Devilliers & 176
  2. Virat Kohli & 183
  3. Dhoni & 183
  4. Gayle & 237

2)Use of Dictionary

As a dictionary, it holds the elements in key-value mapping format and uses hashing internally; as a result, we can easily retrieve a value from the dictionary by its key. In the best-case scenario, its complexity is O(1), while in the worst-case scenario, it can be O(n).

3)Creating a new Dictionary

We can create dictionary by two ways i.e two types of syntaxes.

  1. dict()
  2. {}

It will generate an empty dictionary.
Transfer the key-value pairs in curly braces to build a dictionary of objects. A colon will also be used to divide the key and value in each pair (:).

Let us create a sample dictionary and print the key value pairs in it

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# printing the dictionary
print(dictionary)

Output:

{'This': 100, 'is': 200, 'BTechGeeks': 300}

4)The most important things to know about keys in the dictionary

Keys in the dictionary are always unique, and they must be of an immutable data type, such as strings, numbers, or tuples.
This means that once a key-value pair is added to the dictionary, we cannot change the key itself, but we can change the value associated with it.

For example, suppose we make a dictionary with duplicate keys and print its contents.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'This': 300}
# printing the dictionary
print(dictionary)

Output:

{'This': 300, 'is': 200}

5)The most important things to know about values in the dictionary

A dictionary’s values can be of any type.

Let’s make a dictionary where the key is an integer and the value is a list of strings.

Below is the implementation:

# given dictionary
dictionary = {1: ["HELLO", "THIS", 453], 
              2: ["BTECHGEEKS", 345, 87, 4.5], 
              3: ["PYTHON", True, 3.2, 100]}
# printing the dictionary
print(dictionary)

Output:

{1: ['HELLO', 'THIS', 453], 2: ['BTECHGEEKS', 345, 87, 4.5], 3: ['PYTHON', True, 3.2, 100]}

6)Accessing an element in dictionary

Using the [] operator on the dictionary object, we can access a specific item/pair in a dictionary. If we use the dictionary object’s operator [] and pass a key, it will return its value as shown below.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'This': 300}
# printing the element value in dictionary
print(dictionary['is'])

Output:

200

Now when we pass a key which does not exist in the dictionary, a KeyError is returned

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# printing the element value in dictionary
print(dictionary['python'])

Output:

Traceback (most recent call last):
  File "/home/eb2788937138651d15b1bf1915ff16a5.py", line 4, in <module>
    print(dictionary['python'])
KeyError: 'python'

Related Programs: