|
26 | 26 | "#### 4) [Sets](#ch4)\n", |
27 | 27 | "#### 5) [Decorators](#ch5)\n", |
28 | 28 | "#### 6) [Threading](#ch6)\n", |
29 | | - "#### 7) [`__slot__` ](#ch7)\n", |
30 | | - "#### 8) [Collections ](#ch8)" |
| 29 | + "#### 7) [`__slot__`](#ch7)\n", |
| 30 | + "#### 8) [Collections](#ch8)\n", |
| 31 | + "#### 9) [Enumerate](#ch9)" |
31 | 32 | ] |
32 | 33 | }, |
33 | 34 | { |
|
2325 | 2326 | "And thats all. Don't miss out [Python Collection](https://docs.python.org/3/library/collections.html) documentation." |
2326 | 2327 | ] |
2327 | 2328 | }, |
| 2329 | + { |
| 2330 | + "cell_type": "markdown", |
| 2331 | + "metadata": {}, |
| 2332 | + "source": [ |
| 2333 | + "<a id=\"ch9\"></a>\n", |
| 2334 | + "## Chapter 9 - Enumerate\n", |
| 2335 | + "\n", |
| 2336 | + "The `enumerate()` method adds a counter to an iterable and returns it in a form of enumerate object. " |
| 2337 | + ] |
| 2338 | + }, |
2328 | 2339 | { |
2329 | 2340 | "cell_type": "code", |
2330 | | - "execution_count": null, |
| 2341 | + "execution_count": 8, |
2331 | 2342 | "metadata": {}, |
2332 | | - "outputs": [], |
2333 | | - "source": [] |
| 2343 | + "outputs": [ |
| 2344 | + { |
| 2345 | + "name": "stdout", |
| 2346 | + "output_type": "stream", |
| 2347 | + "text": [ |
| 2348 | + "1 Neur\n", |
| 2349 | + "2 Schweinsteiger\n", |
| 2350 | + "3 Lahm\n", |
| 2351 | + "4 Muller\n" |
| 2352 | + ] |
| 2353 | + } |
| 2354 | + ], |
| 2355 | + "source": [ |
| 2356 | + "my_list = ['Neur', 'Schweinsteiger', 'Lahm', 'Muller']\n", |
| 2357 | + "for c, value in enumerate(my_list, 1):\n", |
| 2358 | + " print(c, value)" |
| 2359 | + ] |
| 2360 | + }, |
| 2361 | + { |
| 2362 | + "cell_type": "markdown", |
| 2363 | + "metadata": {}, |
| 2364 | + "source": [ |
| 2365 | + "Note that `(my_list, 1)` indicates the starting index of the enumerated list." |
| 2366 | + ] |
| 2367 | + }, |
| 2368 | + { |
| 2369 | + "cell_type": "markdown", |
| 2370 | + "metadata": {}, |
| 2371 | + "source": [ |
| 2372 | + "You can also create tuples containing the index and list item using a list. " |
| 2373 | + ] |
| 2374 | + }, |
| 2375 | + { |
| 2376 | + "cell_type": "code", |
| 2377 | + "execution_count": 6, |
| 2378 | + "metadata": {}, |
| 2379 | + "outputs": [ |
| 2380 | + { |
| 2381 | + "name": "stdout", |
| 2382 | + "output_type": "stream", |
| 2383 | + "text": [ |
| 2384 | + "[(1, 'Neur'), (2, 'Schweinsteiger'), (3, 'Lahm'), (4, 'Muller')]\n" |
| 2385 | + ] |
| 2386 | + } |
| 2387 | + ], |
| 2388 | + "source": [ |
| 2389 | + "my_list = ['Neur', 'Schweinsteiger', 'Lahm', 'Muller']\n", |
| 2390 | + "counter_list = list(enumerate(my_list, 1))\n", |
| 2391 | + "print(counter_list)" |
| 2392 | + ] |
| 2393 | + }, |
| 2394 | + { |
| 2395 | + "cell_type": "markdown", |
| 2396 | + "metadata": {}, |
| 2397 | + "source": [ |
| 2398 | + "That's it for today!" |
| 2399 | + ] |
2334 | 2400 | } |
2335 | 2401 | ], |
2336 | 2402 | "metadata": { |
|
0 commit comments