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:

Paddy3118 said...

Or:
my_list[::-1]

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

- Paddy.

dustbunny said...

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.