'How to rotate a tuple?

I have a function with a tuple, example

    def rotate_block(block: typing.Tuple[float, float]):
        """
        Rotates a block
        :param block: a block (width_block, height_block)
        """
        return tuple([block[1], block[0]])

How do I return the block rotated? I tried this code

[(t[1], t[0]) for t in block]

But it doesnt work, why?

My output

Traceback (most recent call last):
  File "C:\Users\xxx\Desktop\algorithms\test_unit.py", line 18, in test_rotate_block
    self.assertEqual(tg.rotate_block((3,1)), (1,3))
  File "C:\Users\xxx\Desktop\algorithms\xx.py", line 31, in rotate_block
    [tuple(reversed(t)) for t in block]
  File "C:\Users\xxx\Desktop\algorithms\xx.py", line 31, in <listcomp>
    [tuple(reversed(t)) for t in block]
TypeError: 'int' object is not reversible


Solution 1:[1]

You don't have to do anything special, just use tuple unpacking and then return the block in reversed order:

import typing


def rotate_block(block: typing.Tuple[float, float]):
    width, height = block

    return height, width


input_block = (3.14, 2.71)

print(rotate_block(input_block))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Constantinescu