Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combining two arguments of a function

So I started learning python in my free time, because of covid.
Yesterday I tried creating a function that converts two arguments into one list and combines them.
For example I created a function: sumup((1,2,3),(4,5,6)) that returns [1, 2, 3, 4, 5, 6]. This was fairly easy to do.

def sumup(arg1, arg2):
    combine_1 = list(arg1 + arg2)

    return combine_1

But now im trying to figure out how I can combine tuples and lists. So sumup((1,2,3), [565]) should return [1,2,3,565].

I’d be glad if someone could give me some hints.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I am not sure if this is what you want to accomplish, but at a glance, I would suggest that you convert your tuple to a list in order to concatenate both lists. Here is an illustration:

def merge_tuple_and_list(tuple, mlist):
    return np.concatenate([list(tuple), mlist])

print(merge_tuple_and_list((1,2,3), [565]))
print(type(merge_tuple_and_list((1,2,3), [565])))

Output:

array([  1,   2,   3, 565])
<class 'numpy.ndarray'>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading