[Tutor] Rearrange a list

Mats Wichmann mats at wichmann.us
Fri May 1 08:20:46 EDT 2020


On 5/1/20 2:38 AM, EK Esawi via Tutor wrote:
> Hi All--,
> 
> I have a list, e.g. x=[1,2,3,4,5,6,7,8,9,10,……]. I want to rearrange the order of the positions of the list values as follow [1,2,4,3,5,6,8,7,9,10,12,13,15……]. Or something like this  [1,[2,4],[3,5],[6,8],[7,9],[10,12],……] which then can be flattened. I am not concerned about the 1st item on the list. I am hoping that this can be done vis list comprehension or something like it w/o using other Python modules.
> 
> Here is my attempt but the resulting pairing is wrong. It contains unwanted pairs such as [4,6] and [8,10]

I think you need to describe the problmm accurately in words before you
can see how to approach how to translate that into Python.

Clearly this forms part of the translated description (but do it in
words anyway, as part of the process):

b=[[i, i + 2] for i in range(1,len(x) - 1)]

But your translation of into code misses anything which reflects that
[4, 6] would be an invalid pair.  Please describe why it is so.






More information about the Tutor mailing list