Python Dict And Set Example

1. Python Dict Introduction.

Python has a built-in dictionary: dict. It is also called map in other coding languages, using key-value storage, it has an extremely fast lookup speed. For example, suppose you want to find the corresponding score according to the student’s name. If you use list, you need two lists.

names = ['Tom', 'Bob', 'Jerry']
scores = [85, 95, 100]

Given a name, to find the corresponding scores, you must first find the corresponding locations in names list and then take the corresponding score from scores list. The longer the list, the longer the time it takes.

If implemented with dict, you only need a comparison table of “name” – “score”, and look up the score directly by name, no matter how large the table is, the search speed will not slow down. Write a dict in Python as follows:

>>> d = {'Tom': 85, 'Bob': 85, 'Jerry': 95}
>>> d['Jerry']
95

Why is dict searching so fast? Because dict is implemented as a dictionary. Suppose the dictionary contains 10,000 words, and we want to look up a word, and one way to do that is to turn the dictionary back from the first page until we find the word we want, and that’s the way to look up elements in the list, the bigger the list, the slower the search.

The second method is to look up the page number of the word in the index table of the dictionary (such as the head table), and then go straight to the page to find the word. No matter which word you’re looking for, it’s very fast and it doesn’t slow down as the size of the dictionary increases.

Dict is the second implementation. Given a name, such as ‘Jerry’, dict can directly calculate the “page number” of Jerry’s score’s memory address stored by the number 95, which is taken out directly, so the speed is very fast.

Besides put data into dict at initialization time, you can also put data in dict later as following:

>>> d = {'Tom': 85, 'Bob': 85, 'Jerry': 95}
>>> d['Jack'] = 99
>>> d['Jack']
99

Since one key can only map to one value, so putting value into a key multiple times will erase the previous value.

>>> d = {'Tom': 85, 'Bob': 85, 'Jerry': 95}
>>> d['Jack'] = 99
>>> d['Jack']
99
>>> d['Jack'] = 100
>>> d['Jack']
100

If the key does not exist, dict will report an error.

>>> d['Kevin']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Kevin'

There are two ways to avoid the error that the key does not exist.

One is to judge whether the key exists through in operator.

>>> 'Kevin' in d
False

The other is to use dict’s get() method. If the key does not exist, it can return None, or the value specified by the second parameter:

>>> d.get('Kevin')
>>> d.get('Kevin', -1)
-1

Note: Python’s interactive environment does not display results when None is returned.

To remove a key, use the pop(key) method, and the corresponding value is also removed from the dict.

>>> d.pop('Bob')
85
>>> d
{'Tom': 85, 'Jerry': 95}

It is important to note that the order in which dict store data is independent of the key’s put in order.

Compared with list, dict has the following features.

  1. The search and insert speed is extremely fast and will not slow down as the key increases.
  2. It takes up a lot of memory.

The list is in the opposite.

  1. The time to find and insert increases with the list elements increases.
  2. Take up little space.

Dict can be used in many places that require high speed query, and is almost everywhere in Python code. It is important to use dict correctly. The first thing to keep in mind is that the key of dict must be immutable objects.

This is because dict calculates the storage location of value based on the key, and if the same key is calculated each time, the result is different, then the dict interior is completely messed up. This algorithm for calculating positions through key is called Hash.

To make sure the hash is correct, the object as the key cannot change. In Python, strings, integers, and so on are immutable, so you can safely use them as keys. And the list is variable, so it can’t be used as a key.

>>> list_key = ["a", "c", "d"]
>>> d[key] = 'this is a list'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

2. Python Set Introduction.

Set is similar to dict, which is a collection of keys, but does not store value. Because the key cannot be repeated, there is no duplicate key in the set. To create a set, you need to provide a list as an input collection.

>>> s = set([7, 8, 9])
>>> s
{7, 8, 9}

Note that the incoming parameter [7, 8, 9] is a list, and the displayed {7, 8, 9} simply tells you that there are elements 7, 8, 9 within the set, and the displayed order does not mean that the set is ordered.

Duplicate elements are automatically filtered in a set:

>>> s = set([7, 7, 8, 8, 9, 9])
>>> s
{7, 8, 9}

You can add elements to a set using the add(key) method, if you repeated add same key there will only one key is stored.

>>> s.add(10)
>>> s
{7, 8, 9, 10}
>>> s.add(10)
>>> s
{7, 8, 9, 10}

Elements can be removed by the remove(key) method.

>>> s.remove(10)
>>> s
{7, 8, 9}

Set can be regarded as a collection of disordered and non-repeated elements in the mathematical sense, so the two sets can do operations such as intersection and union in the mathematical sense.

>>> s1 = set([7, 8, 9])
>>> s2 = set([8, 9, 10])
>>> s1 & s2
{8, 9}
>>> s1 | s2
{7, 8, 9, 10}

The only difference between set and dict is that there is no corresponding value stored in set, but the set’s principle is the same as dict, so you can’t put mutable objects in set either, because you can’t tell if two mutable objects are equal, and you can’t guarantee that there will be “no repeating elements” within a set. Try putting the list in the set to see whether there is an error or not.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.