Loop in list.

Discussion in 'Python' started by Jim, Feb 8, 2005.

  1. Jim

    Jim Guest

    Where did this type of structure come from:

    mat = ['a' for i in range(3)] ?

    This will produce a list of three elements but
    I don't see reference for it in any of the books.
     
    Jim, Feb 8, 2005
    #1
    1. Advertising

  2. Jim

    Jeremy Jones Guest

    Jim wrote:

    >Where did this type of structure come from:
    >
    >mat = ['a' for i in range(3)] ?
    >
    >This will produce a list of three elements but
    >I don't see reference for it in any of the books.
    >
    >
    >

    It's called a list comprehension and it appeared in Python 2.0.

    http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000



    Jeremy Jones
     
    Jeremy Jones, Feb 8, 2005
    #2
    1. Advertising

  3. "Jim" <> wrote:

    > Where did this type of structure come from:
    >
    > mat = ['a' for i in range(3)] ?
    >
    > This will produce a list of three elements but
    > I don't see reference for it in any of the books.


    it's called "list comprehension", and was added in Python 2.0.

    http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000
    http://docs.python.org/tut/node7.html#SECTION007140000000000000000
    http://docs.python.org/ref/lists.html

    Python 2.4 introduces a variation called "generator expressions":

    http://www.python.org/doc/2.4/whatsnew/node4.html
    http://www.python.org/peps/pep-0289.html
    http://docs.python.org/ref/genexpr.html

    </F>
     
    Fredrik Lundh, Feb 8, 2005
    #3
  4. Jim

    Bill Mill Guest

    Jim,

    That is called a "list comprehension", and it is a feature which
    appeared in python 2.3 (iirc). Thus if your books are about earlier
    versions of python, list comprehensions will not be covered.

    Check out the section of the tutorial about them at
    http://docs.python.org/tut/node7.html#SECTION007140000000000000000 .

    Peace
    Bill Mill
    bill.mill at gmail.com


    On Tue, 08 Feb 2005 06:50:08 -0800 (PST), Jim <> wrote:
    > Where did this type of structure come from:
    >
    > mat = ['a' for i in range(3)] ?
    >
    > This will produce a list of three elements but
    > I don't see reference for it in any of the books.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Bill Mill, Feb 8, 2005
    #4
  5. On Tue, 08 Feb 2005 06:50:31 -0800 (PST), Jim <> wrote:
    > Where did this type of structure come from:
    >
    > mat = ['a' for i in range(3)] ?
    >
    > This will produce a list of three elements but
    > I don't see reference for it in any of the books.


    It's called a "List Comprehension". There's a good historical reason
    for the name, but you can be excused in not having looked under that
    name in the index. See
    <http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000>
    for details.

    They *should* be mentioned in most books, provided that it's vaguely
    recent and covers Python 2.0 or later.

    List comps have a cool new little sister, generator expressions - see
    <http://www.brunningonline.net/simon/blog/archives/001025.html>.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Feb 8, 2005
    #5
  6. Jim wrote:

    > Where did this type of structure come from:
    >
    > mat = ['a' for i in range(3)] ?
    >
    > This will produce a list of three elements but
    > I don't see reference for it in any of the books.


    Its called a list-comprehension. And as I don't know what books you mean, I
    can't say if its covered there or not.

    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Feb 8, 2005
    #6
  7. Jim

    Roy Smith Guest

    Jim <> wrote:
    >Where did this type of structure come from:
    >
    >mat = ['a' for i in range(3)] ?
    >
    >This will produce a list of three elements but
    >I don't see reference for it in any of the books.


    It's a list comprehension. Unfortunately, this is a bad example of
    one, since a much simplier way of writing the same thing would be:

    mat = ['a'] * 3

    After several years of them being in the language, I'm still somewhat
    up in the air about the wisdom of list comprehensions. There is no
    doubt that they're convenient and compact, but I think they make code
    more difficult to read. The functional programming folks will
    certainly disagree, and I realize I'm probably in the minority on
    this, but that's my opinion.
     
    Roy Smith, Feb 8, 2005
    #7
  8. Jim

    Jim Guest

    Thanks for the help. Python is somewhat mysterious to an old fortan
    programer.

    Jim
     
    Jim, Feb 8, 2005
    #8
  9. Jim

    Jim Guest

    Particularly one who can't spell. Fortran.

    Jim
     
    Jim, Feb 8, 2005
    #9
  10. On Tue, 08 Feb 2005 23:07:09 -0500, Caleb Hattingh <> wrote:
    > '>>> a = [i*2*b for i in range(3) for b in range(4)]
    > '>>> a
    > [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]
    >
    > Might take you a while to correlate the answer with the loop, but you
    > should be able to see after a while that this nesting is the same as
    >
    > '>>> a = []
    > '>>> for b in range(4):
    > '>>> for i in range(3):
    > '>>> a.append(i*2*b)


    There is a subtle error in this explanation. The equivilence actually
    looks like:

    '> a = []
    '> l1 = range(4)
    '> l2 = range(3)
    '> for b in l1:
    '> for i in l2:
    '> a.append(i*2*b)

    Stephen
     
    Stephen Thorne, Feb 8, 2005
    #10
  11. Jim a écrit :
    > Where did this type of structure come from:
    >
    > mat = ['a' for i in range(3)] ?
    >
    > This will produce a list of three elements but
    > I don't see reference for it in any of the books.
    >

    Now everyone told you *what* is it, I'll (very very dumbly) answer the
    question : this syntax comes from Haskell.

    HTH !-)
    Bruno
     
    Bruno Desthuilliers, Feb 8, 2005
    #11
  12. Stephen Thorne wrote:

    >> '>>> a = [i*2*b for i in range(3) for b in range(4)]
    >> '>>> a
    >> [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]
    >>
    >> Might take you a while to correlate the answer with the loop, but you
    >> should be able to see after a while that this nesting is the same as
    >>
    >> '>>> a = []
    >> '>>> for b in range(4):
    >> '>>> for i in range(3):
    >> '>>> a.append(i*2*b)

    >
    > There is a subtle error in this explanation.


    if you run the example, you'll notice that it's not so subtle. read on.

    > The equivilence actually looks like:
    >
    > '> a = []
    > '> l1 = range(4)
    > '> l2 = range(3)
    > '> for b in l1:
    > '> for i in l2:
    > '> a.append(i*2*b)


    really?

    def myrange(x):
    print "RANGE", x
    return range(x)

    print [i*2*b for i in myrange(3) for b in myrange(4)]

    a = []
    for b in myrange(4):
    for i in myrange(3):
    a.append(i*2*b)
    print a

    a = []
    l1 = myrange(4)
    l2 = myrange(3)
    for b in l1:
    for i in l2:
    a.append(i*2*b)
    print a

    prints

    RANGE 3
    RANGE 4
    RANGE 4
    RANGE 4
    [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]
    RANGE 4
    RANGE 3
    RANGE 3
    RANGE 3
    RANGE 3
    [0, 0, 0, 0, 2, 4, 0, 4, 8, 0, 6, 12]
    RANGE 4
    RANGE 3
    [0, 0, 0, 0, 2, 4, 0, 4, 8, 0, 6, 12]

    (to translate a list comprehension to nested statements, remove
    the result expression, insert colons and newlines between the for/if
    statement parts, and put the append(result expression) part inside
    the innermost statement)

    </F>
     
    Fredrik Lundh, Feb 8, 2005
    #12
  13. Jim

    Roy Smith Guest

    Roy Smith, Feb 8, 2005
    #13
  14. Jim

    Someone on this list (SteveB) helped me quite a bit with a list
    comprehension on a recent thread. Roy said it can be hard to read, and I
    agree in part because I always thought they were hard to read, when in
    actual fact I had just never bothered to learn properly. Here is a
    mini-tutorial:

    e.g. 1: The theory

    '>>> a = [ <item1> for i in <item2> ]

    item2 is iterable (like "range()" in your example)
    item1 is the thing that is going to fill up the resulting list, and item1
    is evaluated at each step of the "for" loop.

    This is the same as
    '>>> a = []
    '>>> for i in <item2>:
    '>>> a.append(<item1>)

    e.g. 2: A real example

    '>>> a = [i*2 for i in range(3)]
    '>>> a
    [0, 2, 4]

    so "i*2" gets evaluated for each step in the "for" loop. The values of
    "i" at each step are [0,1,2], according to how "range" works, so "i*2" is
    what you end up with in the resulting list.

    e.g. 3: They can be nested

    '>>> a = [i*2*b for i in range(3) for b in range(4)]
    '>>> a
    [0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]

    Might take you a while to correlate the answer with the loop, but you
    should be able to see after a while that this nesting is the same as

    '>>> a = []
    '>>> for b in range(4):
    '>>> for i in range(3):
    '>>> a.append(i*2*b)

    keep well
    Caleb
     
    Caleb Hattingh, Feb 9, 2005
    #14
  15. Stephen

    You're gonna have to help me here.....what is the effective difference?

    Thanks
    Caleb

    >> '>>> a = []
    >> '>>> for b in range(4):
    >> '>>> for i in range(3):
    >> '>>> a.append(i*2*b)

    >
    > There is a subtle error in this explanation. The equivilence actually
    > looks like:
    >
    > '> a = []
    > '> l1 = range(4)
    > '> l2 = range(3)
    > '> for b in l1:
    > '> for i in l2:
    > '> a.append(i*2*b)
    >
    > Stephen
     
    Caleb Hattingh, Feb 9, 2005
    #15
  16. Hi Fredrik

    *sigh* I think I will stop writing mini-tutorials :)

    You are, of course, correct. And I really like your method of
    explaining how to mentally juggle the LC into explicit loops.

    I shudder to think how mnay people I confused with my incorrect
    examples - I really should have tested them first.

    Thanks again
    Caleb

    > (to translate a list comprehension to nested statements, remove
    > the result expression, insert colons and newlines between the for/if
    > statement parts, and put the append(result expression) part inside
    > the innermost statement)
    >
    > </F>
     
    Caleb Hattingh, Feb 9, 2005
    #16
  17. Jim

    Jim Guest

    Wow! All I wanted to do was write the equivalence
    of the Fortran statement: Real*4 matrix(n,n).

    I'm going to have to go to the intrepreter to see what
    your saying.

    Thanks for all the help.

    Jim
     
    Jim, Feb 10, 2005
    #17
  18. Jim

    Guest

    Jim wrote:
    > Wow! All I wanted to do was write the equivalence
    > of the Fortran statement: Real*4 matrix(n,n).


    If you are doing numerical linear algebra in Python, you should use the
    Numeric or Numarray modules. With Numeric, the equivalent is just

    from Numeric import zeros
    matrix = zeros([n,n],Float)
     
    , Feb 10, 2005
    #18
  19. Jim

    Jim Guest

    I did appreciate the reference. I started with Fortran
    on an IBM (7040 maybe, not sure) using keypunched cards. Some of the
    concepts of the newer languages take some to seem useable.

    Jim
     
    Jim, Feb 10, 2005
    #19
  20. Jim

    Jim Guest

    I assume this is one of the addons for Python. I know that there
    is a great deal of stuff out there available for Python that does
    some of the stuff that I am looking at, but I am interested in
    learning to use Python. When I want to get faster and more
    general, I will get some of this stuff or use a different language.

    Thanks for the help.

    Jim
     
    Jim, Feb 10, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. author
    Replies:
    5
    Views:
    621
    author
    Jul 28, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,385
    Karl Seguin
    Feb 16, 2005
  3. Replies:
    5
    Views:
    602
    benben
    Jan 31, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    434
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page