reordering elements of a list
Roberto Bonvallet
rbonvall at gmail.com
Sat Jun 3 21:06:12 EDT 2006
3 Jun 2006 17:46:49 -0700, greenflame <alikakakhel at yahoo.com>:
> Suppose the main list is: mainlist = list('qwertyuiop')
>
> Suppose the ordering list is: orderinglist = [3, 4, 2, 1]
>
> Then I am looking for a function that will take mainlist and
> orderinglist as arguments and return the following list:
>
> ['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']
>>> mainlist = list('qwertyuiop')
>>> orderinglist = [3, 4, 2, 1]
>>> [mainlist[i - 1] for i in orderinglist] + mainlist[len(orderinglist):]
['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']
Best regards.
--
Roberto Bonvallet
More information about the Python-list
mailing list