Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43682

Re: a couple of things I don't understand wrt lists

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: a couple of things I don't understand wrt lists
Date 2013-04-16 16:43 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <kkjv3b$9pj$1@reader1.panix.com> (permalink)
References <mailman.668.1366126626.3114.python-list@python.org>

Show all headers | View raw


In <mailman.668.1366126626.3114.python-list@python.org> aaB <mecagonoisician@gmail.com> 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"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

a couple of things I don't understand wrt lists aaB <mecagonoisician@gmail.com> - 2013-04-16 17:37 +0200
  Re: a couple of things I don't understand wrt lists John Gordon <gordon@panix.com> - 2013-04-16 16:43 +0000
  Re: a couple of things I don't understand wrt lists Larry Hudson <orgnut@yahoo.com> - 2013-04-16 21:57 -0700
    Re: a couple of things I don't understand wrt lists Serhiy Storchaka <storchaka@gmail.com> - 2013-04-17 12:35 +0300
      Re: a couple of things I don't understand wrt lists 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-17 09:10 -0700
        Re: a couple of things I don't understand wrt lists Ned Batchelder <ned@nedbatchelder.com> - 2013-04-17 14:36 -0400
          Re: a couple of things I don't understand wrt lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-18 04:46 +1000
        Re: a couple of things I don't understand wrt lists Chris Angelico <rosuav@gmail.com> - 2013-04-18 04:42 +1000
      Re: a couple of things I don't understand wrt lists 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-17 09:10 -0700

csiph-web