Skip to content

Commit bad4bee

Browse files
committed
📝 Update types section
1 parent a827b87 commit bad4bee

File tree

4 files changed

+123
-61
lines changed

4 files changed

+123
-61
lines changed

docs/appendix/checks.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ Checks
280280
>>> print(sheet[("A", 1)])
281281
2
282282
283+
* How can you remove all duplicates from a list without changing the order of the
284+
elements in the list?
285+
286+
The keys of a :doc:`/types/dicts` can be used for this:
287+
288+
.. code-block:: pycon
289+
290+
>>> list(dict.fromkeys(l))
291+
283292
:doc:`/types/strings/index`
284293
---------------------------
285294

docs/types/dicts.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,6 @@ Checks
137137
* You can use a :doc:`dictionary </types/dicts>` and use it like a spreadsheet
138138
sheet by using :doc:`tuples </types/sequences-sets/tuples>` as key row and
139139
column values. Write sample code to add and retrieve values.
140+
141+
* How can you remove all duplicates from a list without changing the order of the
142+
elements in the list?

docs/types/sequences-sets/lists.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ has any effect on the original list:
532532
>>> sup
533533
[[0], 1]
534534
535+
.. _check-list:
536+
535537
Checks
536538
------
537539

@@ -558,7 +560,5 @@ Checks
558560
* If you have a nested list ``ll``, how can you get a copy ``nll`` of this list
559561
in which you can change the elements without changing the contents of ``ll``?
560562

561-
.. _check-list:
562-
563563
* Make sure that the ``my_collection`` object is a list before you try to append
564564
data to it.

docs/types/sequences-sets/sets.rst

Lines changed: 109 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,143 @@
11
Sets
22
====
33

4-
Sets in Python are an unordered collection of objects that are used in
5-
situations where membership and uniqueness to the set are the most important
6-
information of the object. The ``in`` operator runs faster with sets than with
7-
:doc:`lists`:
4+
Sets in Python are an unordered collection of objects used in situations where
5+
membership and uniqueness to the set are the most important information of the
6+
object. The ``in`` operator runs faster with sets than with :doc:`lists`:
87

98
.. _set:
109

1110
``set``
1211
-------
1312

13+
Create sets
14+
~~~~~~~~~~~
15+
16+
You can create sets by applying :class:`set` to a sequence, for example to a
17+
:doc:`list <lists>`.
18+
1419
.. code-block:: pycon
15-
:linenos:
1620
17-
>>> x = set([1, 2, 3, 2, 4])
18-
>>> x
19-
{1, 2, 3, 4}
20-
>>> 1 in x
21+
>>> sequences = set(["list", "tuple", "tuple"])
22+
>>> sequences
23+
{'tuple', 'list'}
24+
25+
If a sequence is made into a set, duplicates are removed, but the order is then
26+
also lost.
27+
28+
Individual elements cannot be selected with slicing either:
29+
30+
.. code-block:: pycon
31+
32+
>>> sequences[0]
33+
Traceback (most recent call last):
34+
File "<python-input-27>", line 1, in <module>
35+
sequences[0]
36+
~~~~~~~~~^^^
37+
TypeError: 'set' object is not subscriptable
38+
39+
Check values
40+
~~~~~~~~~~~~
41+
42+
The keyword ``in`` is used to check whether an object belongs to a set.
43+
44+
.. code-block:: pycon
45+
46+
>>> "list" in sequences
2147
True
22-
>>> 5 in x
48+
>>> "set" in sequences
2349
False
24-
>>> x.add(0)
25-
>>> x
26-
{0, 1, 2, 3, 4}
27-
>>> x.remove(4)
28-
>>> x
29-
{0, 1, 2, 3}
30-
>>> y = set([3, 4, 5])
31-
>>> x | y
32-
{0, 1, 2, 3, 4, 5}
33-
>>> x & y
34-
{3}
35-
>>> x ^ y
36-
{0, 1, 2, 4, 5}
37-
>>> x.update(y)
38-
>>> x
39-
{0, 1, 2, 3, 4, 5}
40-
41-
Line 1
42-
You can create a set by applying ``set`` to a sequence, for example to a
43-
:doc:`list <lists>`.
44-
Line 3
45-
When a sequence is made into a set, duplicates are removed.
46-
Lines 4–7
47-
The keyword ``in`` is used to check whether an object belongs to a set.
48-
Lines 8–13
49-
With ``add`` and ``remove`` you can change the elements in set.
50-
Line 15
51-
``|`` is used to get the union or combination of two sets.
52-
Line 17
53-
``&`` is used to get the intersection.
54-
Line 19
55-
``^`` is used to find the symmetrical difference, meaning elements that are
56-
contained in one or the other set, but not in both.
50+
51+
Add and delete values
52+
~~~~~~~~~~~~~~~~~~~~~
53+
54+
You can add and delete values with ``add`` and ``remove``.
55+
56+
.. code-block:: pycon
57+
58+
>>> quantities = sequences.add("set")
59+
>>> quantities
60+
{'list', 'tuple', 'set'}
61+
>>> quantities.remove("set")
62+
>>> quantities
63+
{'list', 'tuple'}
64+
65+
The elements are unordered, which means that the values within a sequence can
66+
shift when new elements are added.
67+
68+
Set formation
69+
~~~~~~~~~~~~~
70+
71+
Union set
72+
.. code-block:: pycon
73+
74+
x = {4, 2, 3, 2, 1}
75+
y = {3, 4, 5}
76+
>>> x.union(y)
77+
{0, 1, 2, 3, 4, 5}
78+
79+
Intersection
80+
.. code-block:: pycon
81+
82+
>>> x.intersection(y)
83+
{3}
84+
85+
Difference or remainder set
86+
87+
.. code-block:: pycon
88+
89+
>>> x.difference(y)
90+
{0, 1, 2}
5791
5892
.. _frozenset:
5993

6094
``frozenset``
6195
-------------
6296

63-
In addition to ``set``, there is also ``frozenset``, which is immutable. This
97+
In addition to ``set``, there is also ``frozenset``, an immutable data type. This
6498
means that they can also be members of other sets:
6599

66100
.. code-block:: pycon
67101
:linenos:
68102
69-
>>> x = set([4, 2, 3, 2, 1])
70-
>>> z = frozenset(x)
71-
>>> z
72-
frozenset({1, 2, 3, 4})
73-
>>> z.add(5)
103+
>>> sequences = frozenset(["list", "tuple", "set", "tuple"])
104+
>>> sequences
105+
frozenset({'list', 'tuple', 'set'})
106+
>>> dicts = {"dict"}
107+
>>> sequences.add(dicts)
74108
Traceback (most recent call last):
75-
File "<stdin>", line 1, in <module>
109+
File "<python-input-18>", line 1, in <module>
110+
sequences.add(dicts)
111+
^^^^^^^^^^^^^
76112
AttributeError: 'frozenset' object has no attribute 'add'
77-
>>> x.add(z)
78-
>>> x
79-
{1, 2, 3, 4, frozenset({1, 2, 3, 4})}
113+
>>> dicts.add(sequences)
114+
>>> dicts
115+
{frozenset({'list', 'tuple', 'set'}), 'dict'}
116+
117+
Performance
118+
-----------
119+
120+
Sets are very fast when checking whether elements are contained in a set. The set
121+
arithmetic of sets is also well suited to finding common and unique values of two
122+
sets. For this purpose, it can be useful to convert :doc:`lists` or :doc:`tuples`
123+
into sets.
80124

81125
Order
82126
-----
83127

84-
However, the speed advantage also comes at a price: sets do not keep the
85-
elements in the correct order, whereas :doc:`lists` and :doc:`tuples` do. If the
86-
order is important to you, you should use a data structure that remembers the
87-
order.
128+
However, the speed advantage also comes at a price: sets do not keep the elements
129+
in the correct order, whereas :doc:`lists` and :doc:`tuples` do. If the order is
130+
important to you, you should only convert the elements into a set for certain
131+
operations, for example to check whether the elements of a list are unique with
132+
133+
.. code-block:: pycon
134+
135+
>>> sequences = ["list", "tuple", "set", "tuple"]
136+
>>> len(sequences) == len(set(sequences))
137+
False
88138
89139
Checks
90140
------
91141

92-
* How many elements does a set have if it is formed from the following list
93-
``[4, 2, 3, 2, 1]``?
142+
* How many elements does a set have if it is formed from the following list ``[4,
143+
2, 3, 2, 1]``?

0 commit comments

Comments
 (0)