The Definitive Guide
13 Mar 2020
Python Dictionaries: The Definitive Guide
Learn all about the Python Dictionary and its potential. You will also learn how to create word frequency using the Dictionary.
If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.
Dictionary is a built-in Python Data Structure that is mutable. It is similar in spirit to List, Set, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys
and can be understood as associative arrays. On an abstract level, it consists of a key
with an associated value
. In Python, the Dictionary represents the implementation of a hash-table
.
You might wonder what are keys?
Well, as shown in the figure below, keys are immutable ( which cannot be changed ) data types that can be either strings or numbers. However, a key can not be a mutable data type, for example, a list. Keys are unique within a dictionary and can not be duplicated inside a dictionary, in case if it is used more than once then subsequent entries will overwrite the previous value.
Key
connects with the value
, hence, creating a map-like structure. If you for a second, remove keys from the picture, all you are left with is a data structure containing a sequence of numbers. Dictionaries, therefore, hold a key: value
pair at each position.
A dictionary is represented by a pair of curly braces {}
in which enclosed are the key: value
pairs separated by a comma.
Let's look at the syntax of a dictionary: dictionary = {"key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}#
Unique Keys
Since now you know that keys
in a dictionary have to be unique, let's understand it with the help of an example.
Let's print this out.
Great, so until now, everything looks fine. You were able to print your first dictionary output. Now, let's repeat the key g
with a new value and see what happens.
As expected, the key g
previous value gamma
was overwritten by value beta
.
Immutable Keys
Now let's see what happens when you try to define the key
as a mutable data type.
From the above output, you can observe that defining the first key of the dictionary as a list results in a TypeError
since dictionary keys must be immutable types and list is a mutable type.
However, there is a workaround to it, which is, replacing the list to a tuple, since a tuple is an immutable data type.
Accessing Keys and Values
Since you now know how to create dictionaries, let's learn to access the keys and the values from the dictionary. To access the key value pair, you would use the .items()
method, which will return a list of dict_items
in the form of a key, value tuple pairs.
To access keys and values separately, you could use a for loop on the dictionary or the .keys()
and .values()
method.
You could even access a value by specifying a key as a parameter to the dictionary.
Nested Dictionary
Creating a nested dictionary is quite simple. In the nested dictionary, you pass in a dictionary inside a dictionary or to put it simply; a dictionary is passed as a value to a key of the main dictionary.
Here you will create a datacamp
dictionary inside a nested dictionary dictionary_nested
.
Dictionary Comprehension
Dictionary comprehensions can be used to create dictionaries from arbitrary key and value expressions. It is a simple and concise way of creating dictionaries and is often faster than the usual for
loop implementations.
Let's print the first ten key: value pairs from the dict_comprehension
dictionary. To achieve this, you will import islice
from the itertools
built-in package and specify n
as a number of key value pairs you want to extract.
As you can see from the above two implementations of a dictionary, dictionary comprehension with a minimal margin, is still the winner in terms of the time it takes to run. You would notice that as you keep increasing the range
parameter, the difference in the time will also increase.
Word Frequency
From a collection of written texts, in this case, a string of text, also known as corpus
, lets us create a word frequency with the help of a dictionary.
Great! So as you can observe from the above output, you were able to get a word count or word frequency from a string of text with the help of a Python Dictionary.
Conclusion
Congratulations on finishing the tutorial.
Please feel free to ask any questions related to this tutorial in the comments section below.
If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.
Reference : https://www.datacamp.com/community/tutorials/dictionary-python
Last updated