0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>A<strong>dictionary</strong>or<strong>associative array</strong>data type is a collection of unique key-value pairs so they do not duplicate.</p>
1
<p>A<strong>dictionary</strong>or<strong>associative array</strong>data type is a collection of unique key-value pairs so they do not duplicate.</p>
2
<h2>The features</h2>
2
<h2>The features</h2>
3
<p>The collections mentioned earlier, namely, lists and tuples, are ordered and store elements in the order we add them. Dictionaries are different. They do not remember the order in which we add items, and instead, store items so that operations on the dictionary contents are as efficient as possible:</p>
3
<p>The collections mentioned earlier, namely, lists and tuples, are ordered and store elements in the order we add them. Dictionaries are different. They do not remember the order in which we add items, and instead, store items so that operations on the dictionary contents are as efficient as possible:</p>
4
<p>Strictly speaking, as of Python 3.7, the internal structure of dictionaries has changed: as of this version, dictionaries remember the order in which we added keys. However, we do not need to tie logic to this property-lists are better for storing the order of things.</p>
4
<p>Strictly speaking, as of Python 3.7, the internal structure of dictionaries has changed: as of this version, dictionaries remember the order in which we added keys. However, we do not need to tie logic to this property-lists are better for storing the order of things.</p>
5
<p>Lists and tuples are<strong>indexed</strong>. It means the position of each element is a<strong>number</strong>- an ordinal number from the beginning of the collection. Dictionary keys do not have to be numbers. They can be many other types of data, most often strings. There are no specific patterns between the dictionary keys except for the uniqueness guarantee, when the indexes of the list and the tuple are consistent with no omissions, and the increment between elements is one.</p>
5
<p>Lists and tuples are<strong>indexed</strong>. It means the position of each element is a<strong>number</strong>- an ordinal number from the beginning of the collection. Dictionary keys do not have to be numbers. They can be many other types of data, most often strings. There are no specific patterns between the dictionary keys except for the uniqueness guarantee, when the indexes of the list and the tuple are consistent with no omissions, and the increment between elements is one.</p>
6
<p>Lists usually are<strong>homogeneous</strong>. They store elements of the same type. On the other hand, dictionaries usually are<strong>heterogeneous</strong>. Both the types of values and the types of keys can be different - yes, this happens, but rarely.</p>
6
<p>Lists usually are<strong>homogeneous</strong>. They store elements of the same type. On the other hand, dictionaries usually are<strong>heterogeneous</strong>. Both the types of values and the types of keys can be different - yes, this happens, but rarely.</p>
7
<h2>The reasons to use dictionaries</h2>
7
<h2>The reasons to use dictionaries</h2>
8
<p>It is convenient to save sets of information about an entity in dictionaries. For example, a user may have a nickname, age, and email address. And we can store all this data in one dictionary:</p>
8
<p>It is convenient to save sets of information about an entity in dictionaries. For example, a user may have a nickname, age, and email address. And we can store all this data in one dictionary:</p>
9
<p>It is what a<strong>dictionary literal</strong>looks like:</p>
9
<p>It is what a<strong>dictionary literal</strong>looks like:</p>
10
<ul><li>Here 'name', 'email' and 'age' are dictionary keys</li>
10
<ul><li>Here 'name', 'email' and 'age' are dictionary keys</li>
11
<li>And 'superbob', 'bob.is.super@mail.com' and 35 are clearly values</li>
11
<li>And 'superbob', 'bob.is.super@mail.com' and 35 are clearly values</li>
12
</ul><p>Similarly, using dictionaries, you can describe any entities in the subject area, for example, an order, a course, a lesson, a topic on a forum, or a comment on a project. It will have its structure in each case, depending on the properties that describe a particular entity.</p>
12
</ul><p>Similarly, using dictionaries, you can describe any entities in the subject area, for example, an order, a course, a lesson, a topic on a forum, or a comment on a project. It will have its structure in each case, depending on the properties that describe a particular entity.</p>
13
<p>In addition, we can use dictionaries as repositories to configure parameters or pass a lot of heterogeneous data to a function in the form of a single parameter.</p>
13
<p>In addition, we can use dictionaries as repositories to configure parameters or pass a lot of heterogeneous data to a function in the form of a single parameter.</p>