Swap values in a tuple/list inside a list in python?

前端 未结 4 711
遥遥无期
遥遥无期 2020-12-09 11:27

I have a tuple/list inside a list like this:

[(\'foo\',\'bar\'),(\'foo1\',\'bar1\'),(\'foofoo\',\'barbar\')]

What is the fastest way in pyt

4条回答
  •  星月不相逢
    2020-12-09 11:37

    Using a list comprehension I find more elegant and understandable to use separate variables instead of indices for a single variable as in the solution provided by @iabdalkader:

    [(b, a) for a, b in mylist]
    

提交回复
热议问题