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
?
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')
>>>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments