Thursday, October 23, 2008

99 problems - python - 5

Reverse a list.

reversed(my_list)

# or for you purists
list(reversed(my_list))

BTW, sorted and reversed are two of my favorite semi-recent additions to python

2 comments:

  1. Or:
    my_list[::-1]

    Gives a copy of my_list in reverse order, as a list.

    - Paddy.

    ReplyDelete
  2. Thanks for your input. As you can see I actually ended up using that in the next problem. I'm a little torn over how I should interpret each of the problems. I'm tempted to just leave things at the iterator level since they are such a nice tool and python is moving that way any way.

    ReplyDelete