-2

What would be the easiest (shortest) way to switch elements in tuple?

my_tuple= (a,b)

After switch:

new_tuple= (b,a)
UrbanCity
  • 1
  • 3

2 Answers2

0

By reversing it:

>>> my_tuple = ('a', 'b')
>>> new_tuple = my_tuple[::-1]
>>> new_tuple
('b', 'a')
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
0
In [32]: my_tuple= ('a','b')

In [33]: tuple(reversed(my_tuple))
Out[33]: ('b', 'a')
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241