Modifying values in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tron.thomas@verizon.net

    #1

    Modifying values in a list

    The following code:

    numbers = [1, 2, 3]
    for value in numbers:
    value *= 2
    print numbers

    results in the following output:
    [1, 2, 3]

    The intent of the code was to produce this output:
    [2, 4, 6]

    What is the reason for the output produced?
    What code should be used to obtain the desired output?

  • Björn Lindström

    #2
    Re: Modifying values in a list

    tron.thomas@ver izon.net writes:
    [color=blue]
    > The following code:
    >
    > numbers = [1, 2, 3]
    > for value in numbers:
    > value *= 2
    > print numbers
    >
    > results in the following output:
    > [1, 2, 3]
    >
    > The intent of the code was to produce this output:
    > [2, 4, 6]
    >
    > What is the reason for the output produced?
    > What code should be used to obtain the desired output?[/color]

    How about this?

    numbers = [1, 2, 3]
    print [x * 2 for x in numbers]

    --
    Björn Lindström <bkhl@stp.lingf il.uu.se>
    Student of computational linguistics, Uppsala University, Sweden

    Comment

    • Brian van den Broek

      #3
      Re: Modifying values in a list

      tron.thomas@ver izon.net said unto the world upon 29/12/05 10:43 AM:[color=blue]
      > The following code:
      >
      > numbers = [1, 2, 3]
      > for value in numbers:
      > value *= 2
      > print numbers
      >
      > results in the following output:
      > [1, 2, 3]
      >
      > The intent of the code was to produce this output:
      > [2, 4, 6]
      >
      > What is the reason for the output produced?
      > What code should be used to obtain the desired output?[/color]

      value is bound in the for loop, but none of that affects the list to
      which numbers is bound.

      One way:

      IDLE 1.1.2[color=blue][color=green][color=darkred]
      >>> numbers = [1, 2, 3]
      >>> numbers = [x * 2 for x in numbers]
      >>> print numbers[/color][/color][/color]
      [2, 4, 6][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH,

      Brian vdB

      Comment

      • Carsten Haese

        #4
        Re: Modifying values in a list

        On Thu, 2005-12-29 at 11:43, tron.thomas@ver izon.net wrote:[color=blue]
        > The following code:
        >
        > numbers = [1, 2, 3]
        > for value in numbers:
        > value *= 2
        > print numbers
        >
        > results in the following output:
        > [1, 2, 3]
        >
        > The intent of the code was to produce this output:
        > [2, 4, 6]
        >
        > What is the reason for the output produced?
        > What code should be used to obtain the desired output?[/color]

        1) Read http://www.effbot.org/zone/python-objects.htm and reread it
        until you understand why your code doesn't work.

        2) Use a list comprehension:
        numbers = [ value*2 for value in numbers ]

        -Carsten


        Comment

        • Paul McGuire

          #5
          Re: Modifying values in a list

          As others have already posted, changing the value of 'value' has
          nothing to do with the list variable 'numbers'. To modify the list in
          place, you need to access its members by index. The following code
          does this:

          numbers = [1,2,3]
          for i in range(len(numbe rs)):
          numbers[i] *= 2
          print numbers

          But this is how a C or Java programmer would approach this task, and is
          not very Pythonic.

          A bit better is to use enumerate (avoids the ugly range(len(blah) )
          looping):

          numbers = [1,2,3]
          for i,val in enumerate(numbe rs):
          numbers[i] = val*2

          This also modifies the list in place, so that if you are modifying a
          very long (and I mean really quite long - long enough to question why
          you are doing this in the first place) list, then this approach avoids
          making two lists, one with the old value and one with the new.

          But the most Pythonic is to use one of the list comprehension forms
          already posted, such as:

          numbers = [ x*2 for x in numbers ]

          -- Paul

          Comment

          Working...