File tree Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Expand file tree Collapse file tree 1 file changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,19 @@ advanced programmers are unaware of it. It allows us to loop over
7
7
something and have an automatic counter. Here is an example:
8
8
9
9
.. code :: python
10
+
11
+ my_list = [' apple' , ' banana' , ' grapes' , ' pear' ]
12
+ for counter, value in enumerate (my_list):
13
+ print counter, value
10
14
11
- for counter, value in enumerate (some_list):
12
- print (counter, value)
15
+ # Output:
16
+ # 0 apple
17
+ # 1 banana
18
+ # 2 grapes
19
+ # 3 pear
13
20
14
- And there is more! ``enumerate `` also accepts an optional argument which
15
- makes it even more useful .
21
+ And there is more! ``enumerate `` also accepts an optional argument that
22
+ allows us to specify the starting index of the counter .
16
23
17
24
.. code :: python
18
25
@@ -26,9 +33,9 @@ makes it even more useful.
26
33
# 3 grapes
27
34
# 4 pear
28
35
29
- The optional argument allows us to tell ``enumerate `` from where to
30
- start the index. You can also create tuples containing the index and
31
- list item using a list. Here is an example:
36
+ An example of where the optional argument of ``enumerate``comes in handy
37
+ is creating tuples containing the index and list item using a list. Here
38
+ is an example:
32
39
33
40
.. code:: python
34
41
You can’t perform that action at this time.
0 commit comments