Python Forum

Full Version: short way to combine tuples and int's
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
if I have four kinds of data like
l1 = 1   #int
l0 = 8   #int
l2 = 2,3 #tuple
l3 = 4,5 #tuple
Is there an elegant single method to combine any two of these data and return a flattened tuple


for example if a method takes in m = l1,l3
it returns (1,4,5)
note that assuming I don't know the data type of elements in m when using this method
maybe there is a special method I can use but I don't know it
* and ** specifiers in the function’s parameter.
This gives you the positional arguments as a tuple and the keyword arguments as a dictionary.
def foo(*args):
   return args

>>> l0 = 8 
>>> l1 = 1 
>>> l2 = 2,3
>>> l3 = 4,5

>>> foo(l1, l3)
(1, (4, 5))
>>> foo(l0, l1, l2, l3)
(8, 1, (2, 3), (4, 5))
Thank you!

But what if I want (4,5) to be unpacked in the result ?

so (1,4,5) instead of (1,(4,5))
Why does it need to be short? Why not just use an if statement?
Hmm...
Would one if statement be enough? there are three possibilities and they all need different treatment to get the desired result, right ? I mean something like (1,2,3,4,5)

Just asking if there are shorter ways to do this, if not, I'll use the if statement then
Is it possible to start with all tuples, for example

>>> a = 1,
>>> b = (2, 3)
>>> c = a + b
>>> c
(1, 2, 3)
>>>
Thanks!
Yeah, that is what I'm thinking, I guess I can try to find the int element in m, convert it to a tuple in m, then flatten the multi dimensional tuple