I'd like to combine two lists. If I have the following two lists: {a,b,c,d}
and {1,2,3,4}
what do I need to do so that I get {{a,1}, {b,2}, {c,3}, {d,4}}
?
In python, to sort list of tuples by the first element in descending order, we have to use the sort() method with the parameter ” (reverse=True) “ which will sort the elements in descending order.
1) Using tuple() builtin function tuple () function can take any iterable as an argument and convert it into a tuple object. As you wish to convert a python list to a tuple, you can pass the entire list as a parameter within the tuple() function, and it will return the tuple data type as an output.
Here is one way:
Transpose[{{a, b, c, d}, {1, 2, 3, 4}}]
An esoteric method is Flatten
, which (from the Help Section on Flatten) also allows Transpose of a 'ragged' array.
Flatten[ {{a, b, c, d}, {1, 2, 3, 4, 5}}, {{2}, {1}}]
Out[6]= {{a, 1}, {b, 2}, {c, 3}, {d, 4}, {5}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With