You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python_notes/Collections.rst
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
====================================
2
-
Lists, tuples, dictionaries and sets
3
-
====================================
1
+
===========
2
+
Collections
3
+
===========
4
4
5
5
We have already encountered some simple Python types like numbers, strings and booleans. Now we will see how we can group multiple values together in a *collection* -- like a *list* of numbers, or a *dictionary* which we can use to store and retrieve key-value pairs. Many useful collections are built-in types in Python, and we will encounter them quite often.
6
6
@@ -15,6 +15,9 @@ The Python list type is called ``list``. It is a type of sequence -- we can use
15
15
# a list of integers
16
16
numbers = [1, 7, 34, 20, 12]
17
17
18
+
# an empty list
19
+
my_list = []
20
+
18
21
# a list of variables we defined somewhere else
19
22
things = [
20
23
one_variable,
@@ -183,7 +186,7 @@ Many other languages don't have a built-in type which behaves like Python's list
183
186
184
187
Arrays are less easy to use in many ways, but they also have some advantages: because they are so simple, and there are so many restrictions on what you can do with them, the computer can handle them very efficiently. That means that it is often much faster to use an array than to use an object which behaves like a list. A lot of programmers use them when it is important for their programs to be fast.
185
188
186
-
Python has a built-in ``array`` type. It's not quite as restricting as an array in C or Java -- you have to specify a type for the contents of the arrya, and you can only use it to store numeric values, but you can resize it dynamically, like a list. You will probably never need to use it.
189
+
Python has a built-in ``array`` type. It's not quite as restricting as an array in C or Java -- you have to specify a type for the contents of the array, and you can only use it to store numeric values, but you can resize it dynamically, like a list. You will probably never need to use it.
187
190
188
191
Tuples
189
192
======
@@ -196,6 +199,9 @@ We can use tuples in much the same way as we use lists, except that we can't mod
196
199
197
200
animals = ('cat', 'dog', 'fish')
198
201
202
+
# an empty tuple
203
+
my_tuple = ()
204
+
199
205
# we can access a single element
200
206
print(animals[0])
201
207
@@ -269,6 +275,16 @@ It is important to note that unlike lists and tuples sets are *not ordered*. Wh
269
275
270
276
The ``sorted`` function returns a ``list`` object.
271
277
278
+
How do we make an empty set? We have to use the ``set`` function. Dictionaries, which we will discuss in the next section, used curly brackets before sets adopted them, so an empty set of curly brackets is actually an empty dictionary::
279
+
280
+
# this is an empty dictionary
281
+
a = {}
282
+
283
+
# this is how you make an empty set
284
+
b = set()
285
+
286
+
You can use the ``list, ``tuple``, ``dict`` and even ``int``, ``float`` or ``str`` functions in the same way -- they all have sensible defaults -- but you will probably seldom find a reason to do so.
287
+
272
288
Dictionaries
273
289
============
274
290
@@ -349,8 +365,21 @@ You should avoid using ``mykey in mydict.keys()`` to check for key membership, h
349
365
Converting between collection types
350
366
===================================
351
367
352
-
* remember string
353
-
* an empty set with set()
368
+
Implicit conversions
369
+
--------------------
370
+
371
+
If you try to iterate over a collection in a ``for`` loop (something we will discuss in the next section), Python will try to convert it into something that you can iterate over if it knows how to. For example, the dictionary views we saw above are not actually iterators, but Python knows how to make them into iterators -- so you can use them in a ``for`` loop without having to convert them yourself.
372
+
373
+
Sometimes the iterator you get by default may not be what you expected -- if you iterate over a dictionary in a ``for`` loop, you will iterate over the *keys*. If what you actually want to do is iterate over the values, or key and value pairs, you will have to specify that yourself by using the dictionary's ``values`` or ``items`` view instead.
374
+
375
+
Explicit conversions
376
+
--------------------
377
+
378
+
You can convert between the different sequence types quite easily by using the type functions to ``cast`` sequences to the desired types -- just like you would use ``float`` and ``int``
0 commit comments