Pesky reverse()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • engsolnom@ipns.com

    Pesky reverse()


    I need to use the built-in method 'reverse', but notice strange behavior.
    Foe example:

    print my_list.reverse () doesn't work.

    new_list = my_list.reverse () doesn't work.

    my_list.reverse ()
    print my_list does work. (displays reversed list)

    But I want both a 'forward' and 'reverse' list:

    new_list = my+list # should save a 'forward' copy, right? Nope
    my_list.reverse () actually reverses both copies, since Python is a bit too
    helpful sometimes, and I understand why.

    So the question is, how do I get a forward and reverse list?

    Thanks......Nor m.


  • Paul Rubin

    #2
    Re: Pesky reverse()

    engsolnom@ipns. com writes:[color=blue]
    > print my_list.reverse () doesn't work.[/color]

    The reverse() method reverses the list in place and returns None.
    [color=blue]
    > But I want both a 'forward' and 'reverse' list:
    >
    > new_list = my_list # should save a 'forward' copy, right? Nope[/color]

    No, both new_list and my_list are bound to the same list, like in C
    you might have two pointers to the same structure. To make a copy, use

    new_list = my_list[:]
    [color=blue]
    > my_list.reverse () actually reverses both copies, since Python is a bit too
    > helpful sometimes, and I understand why.
    >
    > So the question is, how do I get a forward and reverse list?[/color]

    new_list = my_list[:]
    new_list.revers e()

    Comment

    • Sean Ross

      #3
      Re: Pesky reverse()

      <engsolnom@ipns .com> wrote in message
      news:3t3svvs7ni 8l8jnn1l1v60oog irqoaa85f@4ax.c om...[color=blue]
      > I need to use the built-in method 'reverse', but notice strange behavior.[/color]

      [snip]
      [color=blue]
      > new_list = my+list # should save a 'forward' copy, right? Nope
      > my_list.reverse () actually reverses both copies, since Python is a bit too
      > helpful sometimes, and I understand why.
      >
      > So the question is, how do I get a forward and reverse list?[/color]
      [color=blue][color=green][color=darkred]
      >>> forward = range(10)
      >>> reverse = forward[:] # copy
      >>> reverse.reverse ()
      >>> print forward[/color][/color][/color]
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][color=blue][color=green][color=darkred]
      >>> print reverse[/color][/color][/color]
      [9, 8, 7, 6, 5, 4, 3, 2, 1, 0][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      HTH
      Sean


      Comment

      • Elaine Jackson

        #4
        Re: Pesky reverse()

        >>> list1=[1,2,3][color=blue][color=green][color=darkred]
        >>> list2=list1[:] ## "cloning"
        >>> list1.reverse() ==None[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> list1[/color][/color][/color]
        [3, 2, 1][color=blue][color=green][color=darkred]
        >>> list2[/color][/color][/color]
        [1, 2, 3]


        <engsolnom@ipns .com> wrote in message
        news:3t3svvs7ni 8l8jnn1l1v60oog irqoaa85f@4ax.c om...
        |
        | I need to use the built-in method 'reverse', but notice strange behavior.
        | Foe example:
        |
        | print my_list.reverse () doesn't work.
        |
        | new_list = my_list.reverse () doesn't work.
        |
        | my_list.reverse ()
        | print my_list does work. (displays reversed list)
        |
        | But I want both a 'forward' and 'reverse' list:
        |
        | new_list = my+list # should save a 'forward' copy, right? Nope
        | my_list.reverse () actually reverses both copies, since Python is a bit too
        | helpful sometimes, and I understand why.
        |
        | So the question is, how do I get a forward and reverse list?
        |
        | Thanks......Nor m.
        |
        |


        Comment

        Working...