In this example, we will create a single python function that will accept unlimited lists of numbers and merges them into a single list that contains sorted values as well as no duplicated values. This question is from Codewars which I have further modified it to accept unlimited lists instead of two.

def merge_arrays(*kwargs): 
    merge_list = []
    for li in kwargs:
        merge_list += li
    merge_list = list(dict.fromkeys(merge_list))
    return merge_list

Run the above code with the below line of statement and see the outcome by yourself.

print(merge_arrays([1,2,3,5,6,1], [1,3,5], [9,0]))

If you have a better idea, don’t forget to leave them under the comment box below!

Please follow and like us:


  1. I made this using set:

    def merge_lists(*kwargs):
    merge_set = set()
    for list_i in kwargs:
    for item in list_i:
    return list(merge_set)

  2. Inputs can be any iterables. The temporary merge_lists, the first possibly very large, are not needed. Use set rather than dict to uniquify.

    from itertools import chain

    def merge_iterables(*iters):
    “Return sorted, uniquified, merged iterables.”
    return sorted(set(chain(*iters)))

    print(merge_iterables([1,2,3,5,6,1], (1,3,5), {9,0}, {4:’four’, 7:None, 8:8}))
    #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Leave a Reply

Your email address will not be published. Required fields are marked *