|
3 | 3 | # 🐍 Collections |
4 | 4 | <!-- .element: class="headline" --> |
5 | 5 |
|
6 | | -What **lists and arrays** are in Java, are just **lists** in Python. |
7 | | -What **maps** are in Java, are **dictionaries** in Python. |
8 | | -And yes, **sets** are called the same in Python and Java. |
| 6 | +We will only look into a few collection types of Python. |
| 7 | +Find them all at [docs.python.org/3/library/stdtypes.html](https://docs.python.org/3/library/stdtypes.html) |
| 8 | + |
| 9 | +- **Lists and arrays** in Java ➡ are just `list` in Python (but, there is also `tuple`) |
| 10 | +- **Maps** in Java ➡ are `dict` in Python |
| 11 | +- **Sets** in Java ➡ are also `set` in Python |
9 | 12 |
|
10 | 13 | --- |
11 | 14 |
|
12 | | -## `list` |
| 15 | +## Lists 1/2 |
13 | 16 |
|
14 | 17 | Lists hold multiple elements in a specific order. |
15 | 18 |
|
16 | | -```py |
| 19 | +```py [|3-5|8-9|11|13-14|16-17|] |
| 20 | +# ./python/m05_list_operations.py#L1-L15 |
| 21 | + |
17 | 22 | numbers = [1, 2, 3, 4, 5] |
18 | | -names = ["Max", "Alex", "Anton"] |
| 23 | +names = ["Janine", "Ali", "Alice"] |
19 | 24 | mixed = [1, 2, "Max", 3.141] |
20 | | -``` |
21 | 25 |
|
22 | | -### Basic list operations |
23 | 26 |
|
24 | | -```py |
25 | | -# get element at position |
26 | | -names[0] |
| 27 | +names[0] # get element by index |
| 28 | +names[0] = "Peter" # set element at index |
| 29 | + |
| 30 | +names[-1] # use negative indexes to count from the end |
27 | 31 |
|
28 | | -# use negative indexes to count from the end |
29 | | -names[-1] |
| 32 | +names.append(-5) # add element to the end of the list |
| 33 | +names.insert(1, "Bob") # add element at specific index |
30 | 34 |
|
31 | | -# set element at position |
32 | | -names[0] = "Peter" |
| 35 | +names.remove(-5) # remove the first occurrence from the list |
| 36 | +del names[0] # remove by index |
| 37 | +``` |
33 | 38 |
|
34 | | -# add an element to the end of the list |
35 | | -names.append(-5) |
| 39 | +--- |
36 | 40 |
|
37 | | -# add at a specific position |
38 | | -numbers.insert(1, 100) |
| 41 | +## Lists 2/2: More list operations |
39 | 42 |
|
40 | | -# remove the first occurrence from the list |
41 | | -names.remove(-5) |
| 43 | +```py [|3-4|6-7|9-10|] |
| 44 | +# ./python/m05_list_operations.py#L18-L26 |
| 45 | + |
| 46 | +"Alice" in names # check for existence |
| 47 | +"Mario" not in names # ... and non-existence |
| 48 | + |
| 49 | +numbers.count(1) # the number of times this item is in the list |
| 50 | +len(numbers) # total number of elements in the list |
| 51 | + |
| 52 | +# merge two lists into one |
| 53 | +merged = [1, 2, 3] + [4, 5, 6] |
42 | 54 |
|
43 | | -# remove by index |
44 | | -del names[0] |
45 | 55 | ``` |
46 | 56 |
|
| 57 | +<br /> |
| 58 | + |
| 59 | +Learn about all list operations at [docs.python.org/3/tutorial/datastructures.html#more-on-lists](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). |
| 60 | + |
47 | 61 | --- |
48 | 62 |
|
49 | | -### Check for existence of list elements |
| 63 | +## Tuples 1/2 |
50 | 64 |
|
51 | | -```py |
52 | | -# check for existence |
53 | | -"Alex" in names |
| 65 | +Tuples hold two or more objects together in an efficient manner. They have NO direct equivalent in Java. |
| 66 | +Use them to group together small number of elements, e.g., when returning multiple values from a method. |
54 | 67 |
|
55 | | -# ... and not-existence |
56 | | -"Mario" not in names |
| 68 | +```py |
| 69 | +pair = ("Jonas", 12) # create a tuple |
| 70 | +pair[0] # get element by index, similar to lists |
57 | 71 | ``` |
58 | 72 |
|
59 | | -### Merge, count, and check the length of lists |
| 73 | +**Tuples are always immutable!** |
| 74 | +You can NOT modify, append, or delete anything from them after you created one. |
60 | 75 |
|
61 | 76 | ```py |
62 | | -# merging two lists |
63 | | -merged = [1, 2, 3] + [4, 5, 6] |
| 77 | +# pair[1] = 13 |
| 78 | +# 💥 TypeError: 'tuple' object does not support item assignment |
| 79 | + |
| 80 | +# pair.append(123) |
| 81 | +# 💥 AttributeError: 'tuple' object has no attribute 'append' |
| 82 | +``` |
64 | 83 |
|
65 | | -# the number of times this item is in the list |
66 | | -numbers.count(1) |
| 84 | +However, you can convert tuples to lists. |
67 | 85 |
|
68 | | -# total number of elements in the list |
69 | | -len(numbers) |
| 86 | +```py |
| 87 | +numbers = (10, 11, 12) |
| 88 | +list(numbers) |
| 89 | + |
| 90 | +# and back again ... |
| 91 | +letters = ["A", "B", "C"] |
| 92 | +tuple(letters) |
70 | 93 | ``` |
71 | 94 |
|
72 | | -### More list operations |
| 95 | +--- |
| 96 | + |
| 97 | +## Tuples 2/2: Collection Destructuring |
| 98 | + |
| 99 | +**Destructuring** helps you to quickly retrieve elements from list-like types in Python. |
| 100 | + |
| 101 | +```py [|3-6|9-14|17-22|] |
| 102 | +# ./python/m05_destructuring.py |
| 103 | + |
| 104 | +x, y = 1, 2 |
| 105 | +print(x, y) |
| 106 | + |
| 107 | +# > 1 2 |
| 108 | + |
| 109 | + |
| 110 | +numbers = [1, 2, 3] |
| 111 | +x, y, z = numbers |
| 112 | + |
| 113 | +print(x, y, z) |
73 | 114 |
|
74 | | -Learn about all list operations at [docs.python.org/3/tutorial/datastructures.html#more-on-lists](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) |
| 115 | +# > 1 2 3 |
| 116 | + |
| 117 | + |
| 118 | +pair = ("Jonas", 12) |
| 119 | +name, age = pair |
| 120 | + |
| 121 | +print(name, age) |
| 122 | + |
| 123 | +# > Jonas 12 |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +Actually, whenever you use a comma `,` in such contexts, a `tuple` is used implicitly. |
75 | 128 |
|
76 | 129 | --- |
77 | 130 |
|
78 | | -## `dict` |
| 131 | +## Dictionaries |
79 | 132 |
|
80 | 133 | Dictionaries map keys to values. |
81 | 134 |
|
82 | | -```py |
83 | | -my_grades = { |
| 135 | +```py [|3-10|13-15|17-19|21-22|] |
| 136 | +# ./python/m05_dict_operations.py |
| 137 | + |
| 138 | +grades = { |
84 | 139 | "math": 2, |
85 | 140 | "programming": 1, |
86 | 141 | "literature": 3 |
87 | 142 | } |
88 | 143 |
|
89 | 144 | # alternative syntax |
90 | | -my_grades = dict(math=2, programming=1, literature=3) |
91 | | -``` |
| 145 | +grades = dict(math=2, programming=1, literature=3) |
92 | 146 |
|
93 | | -### Basic dict operations |
94 | | - |
95 | | -```py |
96 | | -# get elements by key |
97 | | -my_grades["math"] |
98 | 147 |
|
99 | | -# set elements by key |
100 | | -my_grades["math"] = 5 |
101 | | - |
102 | | -# add a new element |
103 | | -my_grades["electronics"] = 4 |
| 148 | +grades["math"] # get elements by key |
| 149 | +grades["math"] = 5 # set elements by key |
| 150 | +grades["electronics"] = 4 # add a new element |
104 | 151 |
|
105 | 152 | # remove an element (will raise an error if the key does not exist) |
106 | | -if "math" in my_grades: |
107 | | - del my_grades["math"] |
108 | | -``` |
| 153 | +if "math" in grades: |
| 154 | + del grades["math"] |
109 | 155 |
|
110 | | ---- |
| 156 | +grades.keys() # get all the keys as a list |
| 157 | +grades.values() # get all the values as a list |
111 | 158 |
|
112 | | -### Inspect dict elements |
113 | | - |
114 | | -```py |
115 | | -# get all the keys as a list |
116 | | -my_grades.keys() |
117 | | - |
118 | | -# get all the values as a list |
119 | | -my_grades.values() |
120 | 159 | ``` |
121 | 160 |
|
122 | 161 | --- |
123 | 162 |
|
124 | | -## `set` |
| 163 | +## Sets 1/2 |
125 | 164 |
|
126 | 165 | Sets hold multiple elements, without duplicates, but also without order. |
127 | 166 |
|
128 | | -### Basic set operations |
| 167 | +```py [|3|5-6|8|10-12|] |
| 168 | +# ./python/m05_set_operations.py#L1-L10 |
129 | 169 |
|
130 | | -```py |
131 | | -# notice how the '1' is only appended once after all |
132 | | -my_set = {1, 2, 3, 5} |
| 170 | +numbers = {1, 1, 2, 3, 5} # notice how the '1' is only appended once after all |
133 | 171 |
|
134 | | -# check for existence (much faster than with lists) |
135 | | -1 in my_set |
| 172 | +numbers.add(7) # add new elements |
| 173 | +numbers.add(1) # add elements that already exist (no effect) |
136 | 174 |
|
137 | | -# add elements |
138 | | -my_set.add(7) |
| 175 | +1 in numbers # check for existence (much faster than with lists) |
139 | 176 |
|
140 | 177 | # remove elements (will raise an error if the element does not exist) |
141 | | -if 2 in my_set: |
142 | | - my_set.remove(2) |
143 | | -my_set |
| 178 | +if 2 in numbers: |
| 179 | + numbers.remove(2) |
| 180 | +``` |
| 181 | + |
| 182 | +**You can NOT retrieve elements by index from a set!** |
| 183 | + |
| 184 | +```py |
| 185 | +# numbers[0] |
| 186 | +# 💥 TypeError: 'set' object is not subscriptable |
144 | 187 | ``` |
145 | 188 |
|
146 | | -### Retrieve elements from sets |
| 189 | +You must iterate over them or convert the set to a list with `list(elements)` |
147 | 190 |
|
148 | 191 | ```py |
149 | | -# you can NOT retrieve elements by index from a set |
150 | | -my_set[0] |
| 192 | +# ./python/m05_set_operations.py#L12-L14 |
151 | 193 |
|
152 | | -# ... you could convert it to a list with `list(my_set)` or even better: use an iterator |
| 194 | +# iterate over set elements |
| 195 | +for val in numbers: |
| 196 | + print(val) |
153 | 197 | ``` |
154 | 198 |
|
155 | | -### Set arithmetic |
| 199 | +--- |
| 200 | + |
| 201 | +## Sets 2/2: Set Arithmetic |
| 202 | + |
| 203 | +Sets are handy when you want to apply operations from set theory. |
156 | 204 |
|
157 | 205 | ```py |
| 206 | +# ./python/m05_set_arithmetic.py |
| 207 | + |
158 | 208 | a = {1, 2, 3, 4, 5} |
159 | 209 | b = {4, 5, 6, 7, 8} |
160 | 210 |
|
161 | 211 | print("a | b =", a | b) # union |
162 | 212 | print("a & b =", a & b) # intersection |
163 | 213 | print("a - b =", a - b) # difference |
| 214 | + |
| 215 | +# > a | b = {1, 2, 3, 4, 5, 6, 7, 8} |
| 216 | +# > a & b = {4, 5} |
| 217 | +# > a - b = {1, 2, 3} |
| 218 | + |
164 | 219 | ``` |
0 commit comments