Reversing a List
Dave Angel
davea at ieee.org
Wed Sep 1 09:35:53 EDT 2010
Victor Subervi 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. Also, is there a way to append to
> the front of the list directly?
> TIA,
> beno
>
>
The reverse() method reverses that cols object just fine, in place.
Unfortunately, you immediately assign it a new value of None.
Just remove the cols= and it'll work fine.
If you want to understand the problem better, read up on reverse() and
reversed(). They're very different.
In answer to your second question, you could combine the last three
lines as:
cols.insert('Delete', 0)
DaveA
More information about the Python-list
mailing list