Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit4.readnews.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: a couple of things I don't understand wrt lists Date: Tue, 16 Apr 2013 16:43:23 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 53 Message-ID: References: NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1366130603 10035 166.84.1.2 (16 Apr 2013 16:43:23 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Tue, 16 Apr 2013 16:43:23 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:43682 In aaB writes: > but when I do: > for i in rule: > print rule[i] > I get the "complement": > 1 > 1 > 1 > 1 > 0 > 1 > 1 > 1 When you iterate over a list with this statement: for i in rule: i contains each successive list item. However, your code: print rule[i] acts as though i is the list item's *subscript*, which is incorrect. In effect, your code is doing this: print rule[0] print rule[0] print rule[0] print rule[0] print rule[1] print rule[0] print rule[0] print rule[0] Although in your example both rule[0] and rule[1] are zero, so I don't know why '1' ever got printed. Also, as far as I can tell, this code should not have worked at all: for i in range(rule): print rule[i] The range() function expects an integer, not a list. -- John Gordon A is for Amy, who fell down the stairs gordon@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"