list displays

Discussion in 'Python' started by Olive, Jan 8, 2011.

  1. Olive

    Olive Guest

    I am a newbie to python. Python supports what I thinks it is called
    list display, for example:

    [i for i in range(10)]
    [i for i in range(10) if i<6]

    Does anyone know a good documentation for this. I have read the
    language reference but it is confusing.

    Olive
    Olive, Jan 8, 2011
    #1
    1. Advertising

  2. Olive

    Chris Rebert Guest

    On Sat, Jan 8, 2011 at 1:57 PM, Olive <> wrote:
    > I am a newbie to python. Python supports what I thinks it is called
    > list display, for example:
    >
    > [i for i in range(10)]
    > [i for i in range(10) if i<6]
    >
    > Does anyone know a good documentation for this. I have read the
    > language reference but it is confusing.


    You may find the translation to equivalent list-comprehension-free
    code in http://docs.python.org/howto/functional.html#generator-expressions-and-list-comprehensions
    helpful.

    Cheers,
    Chris
    Chris Rebert, Jan 8, 2011
    #2
    1. Advertising

  3. On Sat, 08 Jan 2011 22:57:45 +0100, Olive wrote:

    > I am a newbie to python. Python supports what I thinks it is called list
    > display, for example:
    >
    > [i for i in range(10)]
    > [i for i in range(10) if i<6]


    This is called a list comprehension, not list display.


    > Does anyone know a good documentation for this. I have read the language
    > reference but it is confusing.


    A list comprehension is syntactic sugar for a for loop. If you start with
    code looking like this:

    storage = []
    for i in range(10):
    if i < 6:
    storage.append(i)


    you can re-write this as a list comprehension:

    storage = [i for i in range(10) if i < 6]

    The source doesn't have to be range, it can be any sequence or iterator:

    lengths = [len(obj) for obj in my_list_of_objects]
    # like map(len, my_list_of_objects)


    If you are mathematically inclined, you might also like this analogy: the
    syntax for a list comprehension is similar to that of sets in mathematics.

    [f(x) for x in D]

    is similar to:

    { f(x) ∀ x ∈ D }
    ("the set of f(x) for all x element of D")


    Don't waste your time with list comprehensions that just walk over the
    source, doing nothing. For example:

    [i for i in range(10)]

    Just use list(range(10)) instead.


    Where list comps get complicated is when you combine them. Nested list
    comps are not too bad, although they can get messy:


    [len(s) for s in [str(x) for x in [2**n for n in range(10)]]]


    That's the same as:

    powers_of_two = [2**n for n in range(10)]
    strings = [str(x) for x in powers_of_two]
    lengths = [len(s) for s in strings]


    But what do you make of this?


    [a*b for a in range(3) for b in range(4)]

    This is like a nested for-loop:

    results = []
    for a in range(3):
    for b in range(4):
    results.append(a*b)


    Hope this helps.


    --
    Steven
    Steven D'Aprano, Jan 8, 2011
    #3
    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. Sara
    Replies:
    0
    Views:
    667
  2. VQLC
    Replies:
    0
    Views:
    446
  3. SamIAm
    Replies:
    0
    Views:
    332
    SamIAm
    Jan 26, 2004
  4. Chumley Walrus
    Replies:
    1
    Views:
    12,176
    =?Utf-8?B?QnJpYW4gQnJvd24=?=
    Dec 16, 2004
  5. Shawn K

    dropdown list displays on top of div tags

    Shawn K, Oct 8, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    228
    Robert Koritnik
    Oct 11, 2004
Loading...

Share This Page