1

i'm new to Python but learning with up's and downs.

I don't understand this bit of code:

To iterate over the indices of a sequence, combine range() and len() as follows:

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
...     print i, a[i]
... 
0 Mary
1 had
2 a
3 little
4 lamb

Pretty straight forward. But i don't see in print i, a[i] where the extra [i] comes into play. (Due to my little knowledge of python, no doubt). But if somebody's willing to nudge me in the right direction, i'd be very happy.

4
  • 3
    who/whatever told you to do this should really have told you about enumerate. Commented Nov 17, 2013 at 18:48
  • 2
    @roippi: to be fair, that's the example in the official tutorial. The same section says "In most such cases, however, it is convenient to use the enumerate() function, see Looping Techniques." Commented Nov 17, 2013 at 18:51
  • Welcome to StackOverflow! Please have a look at stackoverflow.com/questions/how-to-ask. The [i] is an index - recall from section 3 of the tutorial that you had word = HelpA resulting in word[4] = A. That's because when you used the index 4 there, you asked for the element in position 4 in word. (Also remember that the leftmost element is in position 0, so word[0] = H, word[1]=e, word[2]=l, word[3]=p, word[4]=A). Commented Nov 17, 2013 at 18:55
  • @amp thank you for the tips. @ all, thank you guys for explaining what it actually does. Commented Nov 17, 2013 at 19:22

3 Answers 3

3

Let's break it down.

The for loop

>>> for i in range(len(a)):
  • len(a) returns an int equal to the length of list a. Let's call this integer b, b = len(a)

  • range(b) returns a list of ints from 0 to b - 1, e.g., [0, 1, 2, 3, 4].

The for loop iterates over the list returned by range() and assigns the current value to i (you could call this whatever)

Example:

>>> for whatever in range(len(a)):
...    print whatever, a[whatever]

the print statement

...    print i, a[i]

Then you print i followed by the value of list a at index i


You could also use the enumerate function as others have suggested:

for key, value in enumerate(a)
    print key, value
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for breaking that down for me. Now it's clear for me how i should read and interpret this code. I'll also take a look at enumerate.
2

Since i is an integer, a[i] is retrieving the ith element from the list a. An easy way to see this -- as pointed out by @DSM -- is to compare what would be output if you did not use [i]:

>>> for i in range(len(a)):
...    print i, a
0 ['Mary', 'had', 'a', 'little', 'lamb']
1 ['Mary', 'had', 'a', 'little', 'lamb']
2 ['Mary', 'had', 'a', 'little', 'lamb']
3 ['Mary', 'had', 'a', 'little', 'lamb']
4 ['Mary', 'had', 'a', 'little', 'lamb']

Instead of printing a single value at each iteration, you are printing out the entire list.

Note that you can achieve the same functionality more concisely using enumerate:

>>> for i, w in enumerate(a):
...    print i, w
0 Mary
1 had
2 a
3 little
4 lamb

2 Comments

+1 for actually explaining what a[i] does, which is the question. The OP can compare the two by comparing print i, a with print i, a[i].
@DSM: thanks for the feedback. I think your idea to compare the two helps makes the functionality clearer, so I added it to my answer.
1

The first of all if you want to get an index and a value in a loop use enumerate

for k, v in emumerate(a):
    print k, v

In your case no extra "[i]" in your sample. len(a) returns 5.

range(5) returns [0, 1, 2, 3, 4]. Assuming that indexes starts with 0 everything is correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.