Python invert every two elements of list

edesz

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?

James

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Iterating over every two elements in a list in Python

From Dev

Convert list into list of tuples of every two elements

From Dev

Plot the intersection in every two list elements

From Dev

Map a function on every two elements of a list

From Dev

Map a function on every two elements of a list

From Dev

Increasing the counter in a list every two elements

From Dev

Getting two consecutive elements from a list every ten elements

From Dev

Combining every two tuples within a list in Python

From Dev

Add quotes to every list elements (all elements contains slash '/') in Python

From Dev

Pair every elements on a list

From Dev

Average of two consecutive elements in the list in Python

From Dev

Apply function to any two elements of a list - Python

From Dev

Access two consecutive elements of a list in Python

From Dev

Python remove elements from two dimensional list

From Dev

Merging two list elements into one in python?

From Dev

Python: fail to swap two elements in a list

From Dev

How to merge two elements in a list in Python

From Dev

Assign two elements and form a list in Python

From Dev

Delete elements of two list with same index in python

From Dev

The most pythonic way to slice a Python list every 100 elements

From Dev

The most pythonic way to slice a Python list every 100 elements

From Dev

Python: adding elements in nested list if the first two elements are the same

From Dev

How to sort list of list on the basis of two elements, in Python?

From Dev

How to sort list of list on the basis of two elements, in Python?

From Dev

Remove every other elements of a list

From Dev

Every two elements mean between these two element

From Dev

List with two elements

From Dev

How to invert the elements of an array?

From Dev

Put elements in two-dimensional list using a loop - Python

Related Related

  1. 1

    Iterating over every two elements in a list in Python

  2. 2

    Convert list into list of tuples of every two elements

  3. 3

    Plot the intersection in every two list elements

  4. 4

    Map a function on every two elements of a list

  5. 5

    Map a function on every two elements of a list

  6. 6

    Increasing the counter in a list every two elements

  7. 7

    Getting two consecutive elements from a list every ten elements

  8. 8

    Combining every two tuples within a list in Python

  9. 9

    Add quotes to every list elements (all elements contains slash '/') in Python

  10. 10

    Pair every elements on a list

  11. 11

    Average of two consecutive elements in the list in Python

  12. 12

    Apply function to any two elements of a list - Python

  13. 13

    Access two consecutive elements of a list in Python

  14. 14

    Python remove elements from two dimensional list

  15. 15

    Merging two list elements into one in python?

  16. 16

    Python: fail to swap two elements in a list

  17. 17

    How to merge two elements in a list in Python

  18. 18

    Assign two elements and form a list in Python

  19. 19

    Delete elements of two list with same index in python

  20. 20

    The most pythonic way to slice a Python list every 100 elements

  21. 21

    The most pythonic way to slice a Python list every 100 elements

  22. 22

    Python: adding elements in nested list if the first two elements are the same

  23. 23

    How to sort list of list on the basis of two elements, in Python?

  24. 24

    How to sort list of list on the basis of two elements, in Python?

  25. 25

    Remove every other elements of a list

  26. 26

    Every two elements mean between these two element

  27. 27

    List with two elements

  28. 28

    How to invert the elements of an array?

  29. 29

    Put elements in two-dimensional list using a loop - Python

HotTag

Archive