Expanding tuples into arguments

Is there a way to expand a Python tuple into a function – as actual parameters? For example, here expand() does the magic: some_tuple = (1, “foo”, “bar”) def myfun(number, str1, str2): return (number * 2, str1 + str2, str2 + str1) myfun(expand(some_tuple)) # (2, “foobar”, “barfoo”) I know one could define myfun as myfun((a, … Read more

Sort a list of tuples by 2nd item (integer value) [duplicate]

This question already has answers here: How to sort a list/tuple of lists/tuples by the element at a given index? (11 answers) Closed 4 years ago. I have a list of tuples that looks something like this: [(‘abc’, 121),(‘abc’, 231),(‘abc’, 148), (‘abc’,221)] I want to sort this list in ascending order by the integer value … Read more

What’s the difference between lists and tuples?

What’s the difference between tuples/lists and what are their advantages/disadvantages? 23 s 23 Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order. Using this distinction makes … Read more

What is the equivalent of the C++ Pair in Java?

In a thread on comp.lang.java.help, Hunter Gratzner gives some arguments against the presence of a Pair construct in Java. The main argument is that a class Pair doesn’t convey any semantics about the relationship between the two values (how do you know what “first” and “second” mean ?). A better practice is to write a very simple class, like the one … Read more