3

I have a list in Python and I am looking to invert every pair of elements in the list. Here is the list:

list_ty = ['many_ord','many','12_FH_Temp_ord','12_FH_Temp','Celsius_ord','Celsius','Pressure_Pas_ord','Pressure_Pas','Kelvin_ord','Kelvin']

Here is what I would like to get:

list_out = ['many','many_ord','12_FH_Temp','12_FH_Temp_ord','Celsius','Celsius_ord','Pressure_Pas','Pressure_Pas_ord','Kelvin','Kelvin_ord']

Additional Information:

There will always be an even number of elements in the list.

The item ending with _ord will always come before the item without _ord.

Question:

Is there a way to reverse the order of each item ending with _ord and the following (associated) item without _ord?

1

5 Answers 5

9

Use a for loop and step 2 elements at a time. You can use the basic python variable swap then:

a, b = b, a

list_ty = ['many_ord','many','12_FH_Temp_ord','12_FH_Temp','Celsius_ord','Celsius','Pressure_Pas_ord','Pressure_Pas','Kelvin_ord','Kelvin']

for i in range(0, len(list_ty), 2):
    list_ty[i], list_ty[i+1] = list_ty[i+1], list_ty[i] 

list_ty has the following value after this loop:

['many', 'many_ord', '12_FH_Temp', '12_FH_Temp_ord', 'Celsius', 'Celsius_ord', 'Pressure_Pas', 'Pressure_Pas_ord', 'Kelvin', 'Kelvin_ord']
0
3

Perhaps a bit silly as a one-liner, but here you go:

from itertools import chain

list_out = list(chain.from_iterable(zip(list_ty[1::2], list_ty[::2])))

To explain: We're using slicing by step 2 with offset 1 to get the values in the odd indices, and another slice by step 2 with offset 0 to get the even indices. We zip them together, making [even, odd, even, odd,...] into ((odd, even), (odd, even), ...), then using chain.from_iterable to flatten out those tuples, and list to realize the chain generator for us.

2

How about this way:

>>> sum(zip(list_ty[1::2], list_ty[::2]), ())
('many', 'many_ord', '12_FH_Temp', '12_FH_Temp_ord', 'Celsius', 'Celsius_ord',     'Pressure_Pas', 'Pressure_Pas_ord', 'Kelvin', 'Kelvin_ord')
>>>
2
  • 1
    Many thanks for this! I like this and it works as I require.
    – edesz
    Sep 29, 2015 at 15:25
  • Just a warning if performance is ever a concern: This will construct and destroy an enormous number of tuples; the two element ones made by zip can be made close to free by using the Py3 version of zip from future_builtins, but it will create a series of len 2, len 4, len 6, etc. tuples, rebuilding once for every pair of elements in list_ty. If the list is known to be smallish, then no big deal, but if it could be longish, then basically all of the other solutions are superior since they avoid creation of tons of temporaries during operation. Sep 29, 2015 at 21:24
2

The lazy way:

>>> [x + y for x in list_ty if not x.endswith('_ord') for y in ('', '_ord')]
['many', 'many_ord', '12_FH_Temp', '12_FH_Temp_ord', 'Celsius', 'Celsius_ord', 'Pressure_Pas', 'Pressure_Pas_ord', 'Kelvin', 'Kelvin_ord']

Ignore any entries that end in "_ord", and just alternatingly add it to the other elements.

2

There are many ways to do this, and here's one with range(len()):

list_out = [list_ty[idx+2*(not idx%2)-1] for idx in range(len(list_ty))]

This goes through each possible index in the original list and grabs the item either before or after that index.

0 would grab the item at index 0 + 2*(not 0%2)-1 or 0 + 2*(not 0)-1 or 0 + 2*1-1 or 1.

1 would grab the item at index 1 + 2*(not 1%2)-1 or 1 + 2*(not 1)-1 or 1 + 2*0-1 or 0.

2 would grab the item at index 2 + 2*(not 2%2)-1 or 2 + 2*(not 0)-1 or 2 + 2*1-1 or 3.

3 would grab the item at index 3 + 2*(not 3%2)-1 or 3 + 2*(not 1)-1 or 3 + 2*0-1 or 2.

As you can see, instead of 0123 it goes 1032, and this pattern will continue until the result is complete.

3
  • 1
    I'm conflicted about this. On the one hand, idx+2*(not idx%2)-1 is absurdly cryptic, relying on amusingly non-intuitive behaviors. On the other hand, I love clever code, even if it's the sort of clever the author doesn't understand a week later. :-) Sep 29, 2015 at 21:55
  • @ShadowRanger - Well, it's the Python code for the algorithm I wanted. I chose not to use a ternary operator, but you could use one and express the indexing as [idx-(1 if idx%2 else -1)] or [idx-1 if idx%2 else idx+1]. If you find those clearer, I'll be happy to add them to the answer. Sep 29, 2015 at 22:39
  • Nah, I wasn't criticizing, that was intended as a compliment more than anything else. Sep 29, 2015 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.