Feed on
Posts
Comments

to iterate over a list in reverse order in python, i use the ‘reverse’ method. i’m sure there are faster ways to do this in terms of running time, but its hard to conceive of anything easier to implement.

here goes: to iterate over ‘my_list’ backwards, write:

my_list.reverse()
for item in my_list:
print item


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

17 Responses to “read a list backwards in python”

  1. on 14 Jun 2007 at 12:02 am Henry

    for i in reversed(my_list):
    print i

    (new builtin from python 2.4)

  2. on 01 Oct 2007 at 2:12 pm Eric Brunson

    Just to show there’s more than one way to do it:

    for i in list[::-1]:
    print i

    :-)

  3. on 24 Jul 2008 at 8:42 pm Zach Dwiel

    correct me if I am wrong, but reversed is the way to go if memory is an issue as the other two solutions first create a copy of the list while rereversed(list) simply returns a reverse iterator?

  4. on 11 Feb 2011 at 10:38 am Paul Man

    If you need the index or the sequence is a string you can always:

    for i in range(len(my_list) – 1, -1, -1):
    print my_list[i]

  5. on 06 Jul 2011 at 4:01 pm paul.reverse

    I would like to emphasize that Zach Dwiel’s solution is side effect free, which he also states.

    In the original solution, if you wanted to print out the list and then process the list again with the original order, then you would need:

    list.reverse()

    # print list in reverse order
    for i in list:
    print i

    list.reverse()

    # print list in original order
    for i in list:
    print i

    where as Zach’s solution allows for the more optimal

    for i in reversed(list):
    print i

    for i in list:
    print i

  6. on 19 Nov 2019 at 9:45 am Chong Kaller

    Been taught a lot. Quite simple to understand. Thanks for sharing with us :)

  7. on 06 Dec 2020 at 12:57 pm sky777

    It would also help if you can do hire ghostwriters who can put forward you with quality content for minimal fee.

    You can learn tons information in discussion forums.
    This crucial part of article writing is often overlooked.

  8. on 09 Jan 2021 at 1:56 am spidercat.org

    Quality articles is the important to be a focus for the
    viewers to pay a quick visit the site, that’s what this website is providing.

  9. on 09 Jan 2021 at 10:38 pm Jenifer

    This software is free of cost and can be utilized each time and wherever you want.

  10. on 10 Jan 2021 at 2:39 am read more

    What’s Going down i’m new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads.
    I’m hoping to contribute & assist different users like its helped me.
    Great job.

  11. Займы с плохой кредитной историей.
    Деньги на карту срочно. Сумма: до 15000 руб.
    … Вы можете получить деньги срочно без процентов, без отказа за 10 минут с мгновенным
    решением, заполнив заявку на займ без проверок.

    Займ онлайн на карту срочно. Получить микрокредит БЕЗ ПРОВЕРОК

  12. on 13 Jan 2021 at 11:44 pm 317socialmedia.com

    Magnificent site. A lot of helpful info here. I am sending it to a few buddies ans
    additionally sharing in delicious. And of course, thanks in your sweat!

  13. on 16 Jan 2021 at 5:14 pm www.momsonterritory.com

    Back linking is a technique to increase website traffic to blog effectively.
    Of course, you need visitors aimed at your website.

    If you have not set one, do it today, at least start formulating one and write
    it down.

  14. on 21 Jan 2021 at 12:06 pm Club388

    If you want to improve your knowledge only keep visiting this web site and
    be updated with the most up-to-date information posted here.

  15. on 24 Jan 2021 at 3:00 pm jeansjacka dam stora storlekar

    Excellent blog right here! Also your website quite a
    bit up very fast! What host are you using? Can I get your affiliate hyperlink to your host?
    I wish my web site loaded up as quickly as yours
    lol

  16. on 25 Jan 2021 at 3:36 am photo sexe

    obviously like your web-site however you have to take a look at the
    spelling on quite a few of your posts. Several of them
    are rife with spelling problems and I in finding it very bothersome to inform the reality nevertheless I will definitely come back again.

  17. on 25 Jan 2021 at 6:00 am hvshops-store.blogspot.com

    Very good post. I certainly love this website. Keep it up!

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.