loop over list and modify in place

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Nogradi

    #1

    loop over list and modify in place

    Is looping over a list of objects and modifying (adding an attribute
    to) each item only possible like this?

    mylist = [ obj1, obj2, obj3 ]

    for i in xrange( len( mylist ) ):
    mylist[i].newattribute = 'new value'


    I'm guessing there is a way to do this without introducing the (in
    principle unnecessary) index i, so what I'm really looking for is a
    looping method which doesn't pass references to the values of the
    items but to the items themselves.
  • James Stroud

    #2
    Re: loop over list and modify in place

    Daniel Nogradi wrote:
    Is looping over a list of objects and modifying (adding an attribute
    to) each item only possible like this?
    >
    mylist = [ obj1, obj2, obj3 ]
    >
    for i in xrange( len( mylist ) ):
    mylist[i].newattribute = 'new value'
    >
    >
    I'm guessing there is a way to do this without introducing the (in
    principle unnecessary) index i, so what I'm really looking for is a
    looping method which doesn't pass references to the values of the
    items but to the items themselves.
    You can use map, or if you don't map, like list comprehension:

    pyclass B(object):
    .... def __repr__(self):
    .... return '<B>: %s' % self.value
    .... def __init__(self):
    .... self.value = None
    ....
    pyalist = [B() for i in xrange(5)]
    pyalist
    [<B>: None, <B>: None, <B>: None, <B>: None, <B>: None]
    py[setattr(b,'valu e',v+5) for (v,b) in enumerate(alist )]
    [None, None, None, None, None]
    pyalist
    [<B>: 5, <B>: 6, <B>: 7, <B>: 8, <B>: 9]
    pymap(setattr, alist, ['value']*5, xrange(5))
    [None, None, None, None, None]
    pyalist
    [<B>: 0, <B>: 1, <B>: 2, <B>: 3, <B>: 4]

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • John Machin

      #3
      Re: loop over list and modify in place


      James Stroud wrote:
      Daniel Nogradi wrote:
      Is looping over a list of objects and modifying (adding an attribute
      to) each item only possible like this?

      mylist = [ obj1, obj2, obj3 ]

      for i in xrange( len( mylist ) ):
      mylist[i].newattribute = 'new value'


      I'm guessing there is a way to do this without introducing the (in
      principle unnecessary) index i, so what I'm really looking for is a
      looping method which doesn't pass references to the values of the
      items but to the items themselves.
      >
      You can use map, or if you don't map, like list comprehension:
      >
      Call me crazy, but isn't the simple construct
      for obj in mylist:
      obj.newattribut e = 'new value'
      what the OP was looking for?

      Comment

      • James Stroud

        #4
        Re: loop over list and modify in place

        John Machin wrote:
        James Stroud wrote:
        >
        >>Daniel Nogradi wrote:
        >>
        >>>Is looping over a list of objects and modifying (adding an attribute
        >>>to) each item only possible like this?
        >>>
        >>>mylist = [ obj1, obj2, obj3 ]
        >>>
        >>>for i in xrange( len( mylist ) ):
        >> mylist[i].newattribute = 'new value'
        >>>
        >>>
        >>>I'm guessing there is a way to do this without introducing the (in
        >>>principle unnecessary) index i, so what I'm really looking for is a
        >>>looping method which doesn't pass references to the values of the
        >>>items but to the items themselves.
        >>
        >>You can use map, or if you don't map, like list comprehension:
        >>
        >
        >
        Call me crazy, but isn't the simple construct
        for obj in mylist:
        obj.newattribut e = 'new value'
        what the OP was looking for?
        >
        I thought he wanted a one liner.


        --
        James Stroud
        UCLA-DOE Institute for Genomics and Proteomics
        Box 951570
        Los Angeles, CA 90095


        Comment

        • Daniel Nogradi

          #5
          Re: loop over list and modify in place

          Call me crazy, but isn't the simple construct
          for obj in mylist:
          obj.newattribut e = 'new value'
          what the OP was looking for?
          Yes, of course. That's why my follow-up post was this:
          Please consider the previous question as an arbitrary random brain
          cell fluctuation whose probability of occurence is around once per
          month and before sending the question it hasn't yet happened to me in
          September.
          :)

          Comment

          • Paul Rubin

            #6
            Re: loop over list and modify in place

            "Daniel Nogradi" <nogradi@gmail. comwrites:
            Is looping over a list of objects and modifying (adding an attribute
            to) each item only possible like this?
            >
            mylist = [ obj1, obj2, obj3 ]
            >
            for i in xrange( len( mylist ) ):
            mylist[i].newattribute = 'new value'
            for m in mylist:
            m.newattribute = 'new value'

            Comment

            Working...