Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am learning Python and came across this example:

W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6))
b = ['a','b','c','d','e','f','g','h','i']
for row in W:
    print b[row[0]], b[row[1]], b[row[2]]

which prints:

a b c

d e f

a e i

c e g

I am trying to figure out why!

I get that for example the first time thru the expanded version is:

print b[(0,1,2)[0]], b[(0,1,2)[1]], b[(0,1,2)[2]]

But I don't understand how the (0,1,2) is interacting. Can anyone offer an explanation? Thanks.

(this is an abbreviated version of some code for a tic tac toe game, and it works well, I just don't get this part)

share|improve this question
add comment

6 Answers

up vote 4 down vote accepted

it iterates over a tuple of tuples, each row is a three-element tuple, when printing it accesses three elements of the b list by index, which is what row tuple contains.

probably, a slightly less cluttered way to do this is:

for f, s, t in W:
    print b[f], b[s], b[t]
share|improve this answer
add comment

In shot, the (0,1,2) does nothing. Its a tuple and can be indexed just like a list, so b[(0,1,2)[0]] becomes b[0] since (0,1,2)[0] == 0.

In the first step Python does b[row[0]]b[(0,1,2)[0]]b[0]'a'

Btw, to get multiple items from a sequence at once you can use a operator:

from operator import itemgetter
for row in W:
    print itemgetter(*row)(b)
share|improve this answer
add comment

Indexing a tuple just extracts the nth element, just as when indexing an array. That is, the expanded version

print b[(0,1,2)[0]], b[(0,1,2)[1]], b[(0,1,2)[2]]

is equal to

print b[0], b[1], b[2]

IE, the 0th element of the (0, 1, 2) tuple ((0, 1, 2)[0]) is 0.

share|improve this answer
add comment

Try to write down the values of all variables in each step: the result you get is right.

interaction 1:

  • row is (0,1,2)
  • b[row[0]], b[row[1]], b[row[2]] is b[(0,1,2)[0], (0,1,2)[1], (0,1,2)[2]], == b[0], b[1], b[2]

interaction 2:

  • row is (3,4,5)
  • b[row[0]], b[row[1]], b[row[2]] is b[3], b[4], b[5]
share|improve this answer
add comment

A Python interactive shell will help you see what is going on:

In [78]: W = ((0,1,2),(3,4,5),(0,4,8),(2,4,6))

In [79]: b = ['a','b','c','d','e','f','g','h','i']

In [81]: row=W[0]       # The first time throught the for-loop, row equals W[0]

In [82]: row
Out[82]: (0, 1, 2)

In [83]: row[0]
Out[83]: 0

In [84]: b[row[0]]
Out[84]: 'a'

In [85]: b[row[1]]
Out[85]: 'b'

In [86]: b[row[2]]
Out[86]: 'c'
share|improve this answer
add comment

for row in W:

first tuple placed into row is (0,1,2)

in other words, W[0] == (0,1,2)

Therefore, since `row` == (0,1,2), then row[0] == 0

So the [0]th element of b == 'a'

b[0] == 'a'

and so on...

b[1] == 'b'
b[2] == 'c'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.6.1206\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflo", "ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.6.1206\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscript>\r\n<script type=\"text/javascript\">var p = \"http\", d = \"static\"; if (document.location.protocol == \"https:\") { p += \"s\"; d = \"engine\"; } var"], "copyright__copyright__copyright": ["c'\nshare|improve this answer\nadd comment\n\nfor row in W:\n\nfirst tuple placed into row is (0,1,2)\n\nin other words, W[0] == (0,1,2)\n\nTherefore, since `row` == (0,1,2), then row[0] == 0\n\nSo the [0]th element of b == 'a'\n\nb[0] == 'a'\n\nand so on...\n\nb[1] == 'b'\nb[2] == 'c'\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}