4 ms·
Tuples are just two or more values with separate types lumped together, i.e. (Int, String). For example, in Haskell, to get the elements of a map, there's this
by pka 9y ago
Tuples are just two or more values with separate types lumped together, i.e. (Int, String).
For example, in Haskell, to get the elements of a map, there's this function:
assocs :: Map k v -> [(k, v)]
which gives you an array of tuples, the first element of which is of type k (the map key type) and the second element being of type v (the map value type.)
- golergka 9y agoI asked a question because I assumed that I might not know something about it, since C# has Tuple<T1, T2> generic class just for that and if it would be just that, Python would have nothing on C# in that regard. But your comment is exactly what I was thinking about. So, is there something else in Python that is not present in C#?
- pka 9y agoI doubt it, other than syntactic sugar, which makes tuples really easy to use, like destructuring and construction. I.e. in Python, if you have a function that returns a tuple: a, b = f(xxx) To create a tuple you just do (a, b). In Haskell, you get some other benefits, like tuples being functors (i.e. "things that can be mapped over"): map show ("tuple", 5) == ("tuple", "5") P.S. Not a Python expert, but I think tuples might be enumerable in Python, which works because Python is dynamically typed.
- klibertp 9y ago> Not a Python expert, but I think tuples might be enumerable in Python Yes, they are. More generally, they are sequences and they support indexing, so they are also iterable. In other words, this Python code works: list(map(str, ("tuple", 5, {}))) == ["tuple", "5", "{}"] There is also a variant of a tuple which assigns names to its positions - it's also iterable. They are also allowed to have any length. Other than that, I'm not aware of any superpowers of Python tuples.