How to reverse a list or string in Python

So easy…

$ python
>>> alpha = “Hello World!”
>>> print alpha
Hello World!
>>> beta = alpha[::-1]
>>> print beta
!dlroW olleH

Works with lists too…

$ python
>>> count = [ 'one', 'two', 'three' ]
>>> countdown = count[::-1]
>>> print countdown
['three', 'two', 'one']

Won’t work with dictionaries but it should work with any hash-able type.

Posted by admica   @   14 April 2010

Related Posts

Like this post? Share it!

Digg Twitter StumbleUpon Delicious Technorati Facebook RSS

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Cat5e or Cat6, which should I get?
Next Post
Microsoft’s Slave Labor at KYE factory in Dongguan China »
Powered by Wordpress   |   Lunated designed by ZenVerse