Reversing a List

Matt Saxton matt at scotweb.co.uk
Wed Sep 1 09:15:54 EDT 2010


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()

> 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>



More information about the Python-list mailing list