List comprehension returning subclassed list type?

Discussion in 'Python' started by Shane Geiger, Mar 25, 2007.

  1. Shane Geiger

    Shane Geiger Guest

    To the best of my understanding, this answers your question:

    iterable
    A container object capable of returning its members one at a
    time. Examples of iterables include all sequence types (such as
    list, str, and tuple) and some non-sequence types like dict and
    file and objects of any classes you define with an __iter__()
    or __getitem__() method. Iterables can be used in a for loop
    and in many other places where a sequence is needed (zip(),
    map(), ...). When an iterable object is passed as an argument
    to the builtin function iter(), it returns an iterator for the
    object. This iterator is good for one pass over the set of
    values. When using iterables, it is usually not necessary to
    call iter() or deal with iterator objects yourself. The for
    statement does that automatically for you, creating a temporary
    unnamed variable to hold the iterator for the duration of the
    loop. See also iterator, sequence, and generator.


    bullockbefriending bard wrote:
    > Given:
    >
    > class Z(object):
    > various defs, etc.
    >
    > class ZList(list):
    > various defs, etc.
    >
    > i would like to be able to replace
    >
    > z_list = ZList()
    > for y in list_of_objects_of_class_Y:
    > z_list.append(y)
    >
    >
    > with something like this:
    >
    > z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]
    >
    > Of course this just gives me a plain list and no access to the
    > methodsof z_list. I could, of course go and write a static method in
    > ZList which takes a plain list of Z objects and returns a ZList.
    >
    > Anyway, my question is whether or not this can be done more elegantly
    > via list comprehension?
    >
    >


    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
     
    Shane Geiger, Mar 25, 2007
    #1
    1. Advertising

  2. Given:

    class Z(object):
    various defs, etc.

    class ZList(list):
    various defs, etc.

    i would like to be able to replace

    z_list = ZList()
    for y in list_of_objects_of_class_Y:
    z_list.append(y)


    with something like this:

    z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]

    Of course this just gives me a plain list and no access to the
    methodsof z_list. I could, of course go and write a static method in
    ZList which takes a plain list of Z objects and returns a ZList.

    Anyway, my question is whether or not this can be done more elegantly
    via list comprehension?
     
    bullockbefriending bard, Mar 25, 2007
    #2
    1. Advertising

  3. On 25 mar, 08:43, "bullockbefriending bard" <>
    wrote:
    > Given:
    >
    > class Z(object):
    > various defs, etc.
    >
    > class ZList(list):
    > various defs, etc.
    >
    > i would like to be able to replace
    >
    > z_list = ZList()
    > for y in list_of_objects_of_class_Y:
    > z_list.append(y)
    >
    > with something like this:
    >
    > z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]
    >
    > Of course this just gives me a plain list and no access to the
    > methodsof z_list. I could, of course go and write a static method in
    > ZList which takes a plain list of Z objects and returns a ZList.
    >
    > Anyway, my question is whether or not this can be done more elegantly
    > via list comprehension?


    Hello,

    A list comprehension will give you a list. But you can use a generator
    expression :

    z_list = ZList(Z(y.var1, y.var2,..)
    for y in list_of_objects_of_class_Y)

    Regards,
    Pierre
     
    Pierre Quentel, Mar 25, 2007
    #3
  4. On Sat, 24 Mar 2007 23:43:10 -0700, bullockbefriending bard wrote:

    > z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]
    >
    > Of course this just gives me a plain list and no access to the
    > methodsof z_list.


    List comprehensions give you a list. If you want to convert that list into
    the type of z_list, you need to do it yourself. Since ZList sub-classes
    from list, probably the easiest way is just:

    z_list = ZList([some list comprehension here])


    > I could, of course go and write a static method in
    > ZList which takes a plain list of Z objects and returns a ZList.


    Yes, that would be one such way. Another way is:

    z_list.extend([some list comprehension here])

    If you are using a recent enough version of Python, you probably don't
    even need the list comprehension. Just use a generator expression:

    z_list.extend(Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y)

    That's especially useful if the list of objects is huge, because it avoids
    creating the list twice: once in the list comp, and once as z_list.



    --
    Steven.
     
    Steven D'Aprano, Mar 25, 2007
    #4
  5. Thanks! I went with extend and generator expression as I *am* dealing
    with rather a lot of data. Now I think I'm going to go on a little
    hunt through my code looking for more places where I should replace
    list comprehensions with generator expressions - bit of a newbie here.

    On Mar 25, 3:57 pm, Steven D'Aprano
    <> wrote:
    > On Sat, 24 Mar 2007 23:43:10 -0700, bullockbefriending bard wrote:
    > > z_list = [Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y]

    >
    > > Of course this just gives me a plain list and no access to the
    > > methodsof z_list.

    >
    > List comprehensions give you a list. If you want to convert that list into
    > the type of z_list, you need to do it yourself. Since ZList sub-classes
    > from list, probably the easiest way is just:
    >
    > z_list = ZList([some list comprehension here])
    >
    > > I could, of course go and write a static method in
    > > ZList which takes a plain list of Z objects and returns a ZList.

    >
    > Yes, that would be one such way. Another way is:
    >
    > z_list.extend([some list comprehension here])
    >
    > If you are using a recent enough version of Python, you probably don't
    > even need the list comprehension. Just use a generator expression:
    >
    > z_list.extend(Z(y.var1, y.var2,..) for y in list_of_objects_of_class_Y)
    >
    > That's especially useful if the list of objects is huge, because it avoids
    > creating the list twice: once in the list comp, and once as z_list.
    >
    > --
    > Steven.
     
    bullockbefriending bard, Mar 25, 2007
    #5
    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. =?Utf-8?B?U2xpbWVtYW4=?=

    Cannot view subclassed Page in Visual Designer

    =?Utf-8?B?U2xpbWVtYW4=?=, Jul 15, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    502
    =?Utf-8?B?U2xpbWVtYW4=?=
    Jul 20, 2005
  2. -
    Replies:
    3
    Views:
    471
    Thomas Fritsch
    Jul 24, 2005
  3. Benji
    Replies:
    1
    Views:
    472
    Chris Uppal
    Oct 6, 2005
  4. Replies:
    20
    Views:
    2,249
  5. Vedran Furac(
    Replies:
    4
    Views:
    355
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...

Share This Page