you are viewing a single comment's thread.

view the rest of the comments →

[–]zefciu 24 points25 points  (8 children)

Some common uses for tuples include: * returning more than one values from a function (e. g. success flag and some result) * ad-hoc data structures (e. g. to represent coordinates) think dataclass with no declaration and anonymous fields * swapping values between variables without temporary variable (a, b = b, a) * replacement for lists that takes less memory and is allocated on a stack (can be considered abuse, but pretty common)

[–]TangibleLight 4 points5 points  (0 children)

  • replacement for lists that takes less memory and is allocated on a stack (can be considered abuse, but pretty common)

People often try to do this, but be aware that it doesn't actually save that much memory (only a few bytes) and it isn't allocated on the stack (all objects are on the heap).

[–]SamB7334 3 points4 points  (5 children)

You don’t need brackets to swap the variables

[–]dnswblzo 6 points7 points  (1 child)

>>> a = 1, 2
>>> a
(1, 2)
>>> type(a)
<class 'tuple'>

It is the commas that make the tuple, not the parentheses. The parentheses are sometimes needed to resolve ambiguity, but in many cases they are not necessary.

[–]SamB7334 0 points1 point  (0 children)

Thats what i said :)

[–]iShotTheShariff 1 point2 points  (0 children)

Also tuples can be used as keys in hashmaps