|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:be423e458dd8b1ac6a36f49330311cc1a1741127c5610a636969efddf6d91597" |
| 4 | + "signature": "sha256:02c6c63beb1de9373d69615a4ba37640a7b01c8f2d088dbfaa84bdaf3452f1c5" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
63 | 63 | "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", |
64 | 64 | "- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n", |
65 | 65 | "- [List comprehensions are fast, but generators are faster!?](#list_generator)\n", |
66 | | - "- [Public vs. private class methods and name mangling](#private_class)" |
| 66 | + "- [Public vs. private class methods and name mangling](#private_class)\n", |
| 67 | + "- [The consequences of modifying a list when looping through it](#looping_pitfall)" |
67 | 68 | ] |
68 | 69 | }, |
69 | 70 | { |
|
1299 | 1300 | ], |
1300 | 1301 | "prompt_number": 28 |
1301 | 1302 | }, |
| 1303 | + { |
| 1304 | + "cell_type": "markdown", |
| 1305 | + "metadata": {}, |
| 1306 | + "source": [ |
| 1307 | + "<br>\n", |
| 1308 | + "<br>\n", |
| 1309 | + "<a name='looping_pitfall'></a>\n", |
| 1310 | + "## The consequences of modifying a list when looping through it" |
| 1311 | + ] |
| 1312 | + }, |
| 1313 | + { |
| 1314 | + "cell_type": "markdown", |
| 1315 | + "metadata": {}, |
| 1316 | + "source": [ |
| 1317 | + "It can be really dangerous to modify a list when iterating through - it is a very common pitfall that can cause unintended behavour!" |
| 1318 | + ] |
| 1319 | + }, |
1302 | 1320 | { |
1303 | 1321 | "cell_type": "code", |
1304 | 1322 | "collapsed": false, |
1305 | 1323 | "input": [ |
1306 | 1324 | "a = [1, 2, 3, 4, 5]\n", |
1307 | 1325 | "for i in a:\n", |
1308 | | - " if not i % 2:\n", |
1309 | | - " a.remove(i)\n", |
| 1326 | + " if not i % 2:\n", |
| 1327 | + " a.remove(i)\n", |
1310 | 1328 | "print(a)" |
1311 | 1329 | ], |
1312 | 1330 | "language": "python", |
|
1320 | 1338 | ] |
1321 | 1339 | } |
1322 | 1340 | ], |
1323 | | - "prompt_number": 1 |
| 1341 | + "prompt_number": 3 |
1324 | 1342 | }, |
1325 | 1343 | { |
1326 | 1344 | "cell_type": "code", |
|
1343 | 1361 | ] |
1344 | 1362 | } |
1345 | 1363 | ], |
1346 | | - "prompt_number": 7 |
| 1364 | + "prompt_number": 4 |
1347 | 1365 | }, |
1348 | 1366 | { |
1349 | | - "cell_type": "code", |
1350 | | - "collapsed": false, |
1351 | | - "input": [ |
1352 | | - "c = [2, 4, 5, 6]\n", |
1353 | | - "for i in c:\n", |
1354 | | - " if i % 2 != 0:\n", |
1355 | | - " c.remove(i)\n", |
1356 | | - "print(c)" |
1357 | | - ], |
1358 | | - "language": "python", |
| 1367 | + "cell_type": "markdown", |
1359 | 1368 | "metadata": {}, |
1360 | | - "outputs": [ |
1361 | | - { |
1362 | | - "output_type": "stream", |
1363 | | - "stream": "stdout", |
1364 | | - "text": [ |
1365 | | - "[2, 4, 6]\n" |
1366 | | - ] |
1367 | | - } |
1368 | | - ], |
1369 | | - "prompt_number": 6 |
| 1369 | + "source": [ |
| 1370 | + "<br>\n", |
| 1371 | + "<br>\n", |
| 1372 | + "**The solution** is that we are iterating through the list index by index, and if we remove one of the items in-between, we inevitably mess around with the indexing, look at the following example, and it will become clear:" |
| 1373 | + ] |
1370 | 1374 | }, |
1371 | 1375 | { |
1372 | 1376 | "cell_type": "code", |
1373 | 1377 | "collapsed": false, |
1374 | 1378 | "input": [ |
1375 | | - "not 4 % 2" |
| 1379 | + "b = [2, 4, 5, 6]\n", |
| 1380 | + "for index, item in enumerate(b):\n", |
| 1381 | + " print(index, item)\n", |
| 1382 | + " if not item % 2:\n", |
| 1383 | + " b.remove(item)\n", |
| 1384 | + "print(b)" |
1376 | 1385 | ], |
1377 | 1386 | "language": "python", |
1378 | 1387 | "metadata": {}, |
1379 | 1388 | "outputs": [ |
1380 | 1389 | { |
1381 | | - "metadata": {}, |
1382 | | - "output_type": "pyout", |
1383 | | - "prompt_number": 9, |
| 1390 | + "output_type": "stream", |
| 1391 | + "stream": "stdout", |
1384 | 1392 | "text": [ |
1385 | | - "True" |
| 1393 | + "0 2\n", |
| 1394 | + "1 5\n", |
| 1395 | + "2 6\n", |
| 1396 | + "[4, 5]\n" |
1386 | 1397 | ] |
1387 | 1398 | } |
1388 | 1399 | ], |
1389 | | - "prompt_number": 9 |
1390 | | - }, |
1391 | | - { |
1392 | | - "cell_type": "code", |
1393 | | - "collapsed": false, |
1394 | | - "input": [], |
1395 | | - "language": "python", |
1396 | | - "metadata": {}, |
1397 | | - "outputs": [] |
| 1400 | + "prompt_number": 7 |
1398 | 1401 | } |
1399 | 1402 | ], |
1400 | 1403 | "metadata": {} |
|
0 commit comments