Activity #25 : Research Use Cases of Python Dictionaries Data Structures

Look up how Python dictionaries are used in programming. A dictionary is a data structure that stores information as key-value pairs. Each key is linked to a specific value, and you can use the key to get the value quickly.
What Is a Dictionary in Python?
A Python dictionary is a data structure that allows us to easily write very efficient code. In many other languages, this data structure is called a hash table because its keys are hashable. We'll understand in a bit what this means.
A Python dictionary is a collection of key:value pairs.You can think about them as words and their meaning in an ordinary dictionary. Values are said to be mapped to keys. For example, in a physical dictionary, the definition science that searches for patterns in complex data using computer methods is mapped to the key Data Science.
In this Python tutorial, you'll learn how to create a Python dictionary, how to use its methods, and dictionary comprehension, as well as which is better: a dictionary or a list. To get the most out of this tutorial, you should be already familiar with Python lists, for loops, conditional statements, and reading datasets with the reader() method. If you aren't, you can learn more at Dataquest.
What Are Python Dictionaries Used for?
Python dictionaries allow us to associate a value to a unique key, and then to quickly access this value. It's a good idea to use them whenever we want to find (lookup for) a certain Python object. We can also use lists for this scope, but they are much slower than dictionaries.
This speed is due to the fact that dictionary keys are hashable. Every immutable object in Python is hashable, so we can pass it to the hash() function, which will return the hash value of this object. These values are then used to lookup for a value associated with its unique key. See the example of the use of the hash() function below:

The string b has a hash value of 2132352943288137677. This value may be different in your case.
How to Create a Dictionary?
But let's stop with the theory and go straight to the dictionary creation. We have two main methods to define a dictionary: with curly braces {} or using the dict() method. We'll create two empty dictionaries:

We can see that both dictionaries have the same data type and are equivalent. Now let's populate a dictionary with keys and values. We can do it by using squared brackets, like this dictionary[key] = value. We can then access the value by using bracket notation with the key we want the value of between the brackets: dictionary[key].

The value of key1 is returned. We can also create a prepopulated dictionary using the syntax below:

Ultimately, another method is using dict(), in which we supply keys and values as a keyword argument list or as a list of tuples:

We used the string data type for the key and the value, but what are the other admissible data types? In Python dictionaries, keys should be hashable objects (even if it's not technically correct, we can also say that the objects should be immutable). Thus, mutable data types like lists, aren't allowed. Let's try to hash() different data types and see what happens:


Integers, floats, strings, and tuples are hashable data types (and they are also immutable) while lists are an unhashable data type (and they are mutable). Python uses hash values to quickly access a dictionary's values.
On the other hand, values can be of whatever type. Let's add more elements to the dictionary using different data types:

{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ['just', 'a', 'list', 'with', 'an', 'integer', 3]{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ['just', 'a', 'list', 'with', 'an', 'integer', 3]
Additionally, we can modify the value of a key with bracket notation that we used to populate a dictionary:

{'key1': 'value1', 'key2': 'value2', 42: 'the answer to the ultimate question of life, the universe, and everything.', 1.2: ['one point two'], 'list': ["it's another", 'list']}
["it's another", 'list']
References:



