Skip to content

Instantly share code, notes, and snippets.

@keefo
Forked from eduzen/merge_lists.py
Created July 7, 2019 17:14
Show Gist options
  • Save keefo/1b54dd5bbe1f5d5df62cef156ef41616 to your computer and use it in GitHub Desktop.
Save keefo/1b54dd5bbe1f5d5df62cef156ef41616 to your computer and use it in GitHub Desktop.
Merge arrays with duplicated elements
def merge_lists(aList, bList):
if not aList or not bList:
print("empty lists")
return aList + bList
c = []
while aList and bList:
if aList[0] < bList[0]:
c.append(aList.pop(0))
else:
c.append(bList.pop(0))
return c + aList + bList
def main():
lista1 = [0,3,7,9,10]
lista2 = [-1, 0, 3, 18, 20]
print(lista1, lista2)
result = merge_lists(lista1, lista2)
print(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment