reordering elements of a list

Roberto Bonvallet rbonvall at gmail.com
Sun Jun 4 20:05:01 EDT 2006


greenflame <alikakakhel at yahoo.com>:
> Roberto: I do not understand the first half of the last line of your
> code.

[mainlist[i - 1] for i in orderinglist] is a list made with the
elements of orderinglist, but instead of taking the actual value i
from the list, the value that is taken is mainlist[i - 1].

If orderinglist is [3, 4, 2, 1], then [mainlist[i - 1] for i in
orderinglist] is:

    [mainlist[3 - 1], mainlist[4 - 1], mainlist[2 - 1], mainlist[1 - 1]]

Remember that indexing starts from 0.
-- 
Roberto Bonvallet



More information about the Python-list mailing list