Python Dictionaries

Here, we will learn about dictionaries, the operators, and methods used on dictionaries. It is nearly inconceivable to work with python program or scripts, without lists and dictionaries. Python dictionaries can be changed easily at runtime.

Dictionary is like a list but in a general form. It can think like this; a dictionary is a mapping between a set of indexes or keys with a set of values, where each key maps to a value. Combination of a key and its value is called a key-value pair or item.

Suppose we take an example and build a dictionary which maps words from English to French. So the keys and their values will be all strings.

In Python dictionary, each key is separated by a colon (:) from its values. Commas separate all the items, and the whole dictionary is enclosed within '{' and '}'. In the dictionary, all the keys should have to be unique with data type as strings, tuples or numbers and the values can be of any of these three types.

Define a Dictionary In Python

Example:
dicto = {'Bookname' : 'Python', 'Price' : 210}

Accessing Dictionary Values

Dictionaries values can be accessed by using the square braces, i.e. [ ] along with the key name to obtain the value. This is multiple item declaration procedures used to declare keys along with their values in Python's dictionary.

Example:
dicto = {'Bookname' : 'Python', 'Price' : 210}
print (dicto['Bookname'])
print (dicto['Price'])

When the above code gets executed the outcome will be:

Output:
Python
260

Creating a New Dictionary in Python

Example:
new_dict = dict()
print (new_dict)

or simply write

new_dict = {}
print (new_dict)
Output:
{}

The function 'dict' creates a new dictionary with no items within it. This is because 'dict' is the name of a built-in function and programmers should avoid in using it as a variable name. Furthermore the curly braces { & }  together represents an empty dictionary. So to use the dictionary to add items Python programmers use square braces such as:

Example:
new_dict = dict()

new_dict['First'] = 10;
new_dict['Second'] = 20;

For mapping a single item of a dictionary, programmers can use this procedure also. This is a one-by-one procedure of declaring items.  The line creates an item that maps from key 'first' to the value 10. Similarly in the second case also.

Dictionary as Set of Counter

Let us consider a situation where programmers are given, and they have to count how many times each letter appears.

To do this, the techniques are:

  • Create twenty-six variables, one for each letter of the alphabet & make the string traverse for each character, incrementing the corresponding counter variable.
  • Using a list, containing twenty-six elements & converting each letter to a specific number & use the number as an index to the list.
  • Programmers can also use dictionaries with letters as keys and counter as their corresponding values and add those items in the dictionary. Then, the programmer can increment the value of an existing item based on the repetition of every single letter.

It is based on the programmer what and how he/she wants to implement things into programming. An implementation is a method of doing or performing a computation based on your conception. In the above scenario, there is an advantage while using dictionaries - that we do not have to think or know ahead of which letter appears in the string and just have to allot space and room for those letters.

Updating Dictionary in Python

Programmers can update or modify the existing dictionary by simply adding a new entry or a key-value pair or by deleting an item or entry.

The code describes it below:

Example:
dicto = {'Bookname' : 'Python', 'Price' : 210}

#Adding new entries
dicto ['Author'] = 'TutorialsCloud' ;
dicto ['Discount']= '10 Percent';

#Updating an Entry
dicto ['Price']  = 200;

Deleting Elements From Dictionary

Deleting elements from a dictionary can be done either removing individual elements or use clear() to clear the entire dictionary's content.

An example is shown below:

Example:
dicto = {'Bookname' : 'Python', 'Price' : 210, 'Author': 'TutorialsCloud'}

del dicto['Price'];
# It deletes only the key with the name 'Price'

dicto.clear();
# The above code removes all entries in dictionary & makes the dictionary empty

del dicto
# The above code Deletes the entire dictionary

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.