quick beginners List comprehension question

Discussion in 'Python' started by Dr Mephesto, Jan 21, 2009.

  1. Dr Mephesto

    Dr Mephesto Guest

    Im new to python, and OOP, and am trying to get a handle on list

    Say I have a class Foo with a property called bar:

    class Foo:
    def __init__(self):
    self.bar = random.randint(1,100)

    and then I make a list of these objects:

    Newlist = []
    for x in range(10):

    Now, suppose I wanted to triple the value of 'bar', I could always do:

    for x in range(10):
    Newlist[x].bar = Newlist[x].bar * 3

    but can I do this using list comprehension? Thanks in Advance!
    Dr Mephesto, Jan 21, 2009
    1. Advertisements

  2. No, as such, because list-comprehensions require you to have an *expression*
    in front of the iteration:

    resultlist = [<expr> for <variable(s)> in <iterable>]

    Now what you of course can do is this:

    def multiply(item):
    item.bar = item.bar * 3

    [multiply(i) for i in items]

    However, doing this will make python produce a list of None-references -
    which is a waste. It's up to you if you care about that, but generally it
    is frowned upon because of that, and the fact that the conciseness of the
    list-comp here isn't really helping with the readability.

    Diez B. Roggisch, Jan 21, 2009
    1. Advertisements

  3. Dr Mephesto

    MRAB Guest

    You could reduce that to:

    for x in Newlist:
    x.bar *= 3

    but I don't think you could do it with list comprehension.
    MRAB, Jan 21, 2009
  4. Other answers have been good; to them I'll add the comment that list
    comprehensions are for *constructing* lists, not manipulating the
    elements thereof.

    Philip Semanchuk, Jan 21, 2009
  5. Dr Mephesto

    MRAB Guest

    If you had:

    def multiply(item):
    item.bar = item.bar * 3
    return item


    [multiply(i) for i in items]

    would return items. Still a bad idea, though, because you're using a
    list comprehension for its side-effect.
    MRAB, Jan 21, 2009
  6. Dr Mephesto

    Lou Pecora Guest

    Well this seems to work just fine. What am I missing:

    print A
    A=[2*a for a in A]
    print A
    Lou Pecora, Jan 21, 2009
  7. Dr Mephesto

    Steve Holden Guest

    The fact that the lists to be multiplied are attributes of a list of
    objects, and therefore aren't themselves a list. Look more closely at
    the original poster's question.

    Steve Holden, Jan 21, 2009
  8. You haven't manipulated the list A, you've simply overwritten it with
    a new list.
    Philip Semanchuk, Jan 21, 2009
  9. Dr Mephesto

    Terry Reedy Guest

    Constructing this list is the appropriate place for a comprehension.
    Newlist = [Foo() for _ in range(10)]
    Use MRAB's replacement for this.
    Don't, for reasons given by others.

    Terry Reedy, Jan 21, 2009
  10. And redundant, which was the reason I ommited it.

    Diez B. Roggisch, Jan 21, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.