Reversing a List

Shashwat Anand anand.shashwat at gmail.com
Wed Sep 1 09:26:36 EDT 2010


On Wed, Sep 1, 2010 at 6:45 PM, Matt Saxton <matt at scotweb.co.uk> wrote:

> On Wed, 1 Sep 2010 09:00:03 -0400
> Victor Subervi <victorsubervi at gmail.com> wrote:
>
> > Hi;
> > I have this code:
> >
> >   cursor.execute('describe products;')
> >   cols = [item[0] for item in cursor]
> >   cols = cols.reverse()
> >   cols.append('Delete')
> >   cols = cols.reverse()
> >
> > Unfortunately, the list doesn't reverse. If I print cols after the first
> > reverse(), it prints None. Please advise.
>
> The reverse() method modifies the list in place, but returns None, so just
> use
> >>> cols.reverse()
>
> rather than
> >>> cols = cols.reverse()
>

Alternatively you can do \,

>>>cols = reversed(cols)


>
> > Also, is there a way to append to
> > the front of the list directly?
> > TIA,
> > beno
>
> The insert() method can do this, i.e.
> >>> cols.insert(0, 'Delete')
>
> --
> Matt Saxton <matt at scotweb.co.uk>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100901/c977a32e/attachment.html>


More information about the Python-list mailing list