Skip to content

Instantly share code, notes, and snippets.

@Prodge
Last active August 12, 2016 03:06
Show Gist options
  • Save Prodge/191164c41ba54202c7c7c785d03ccfbd to your computer and use it in GitHub Desktop.
Save Prodge/191164c41ba54202c7c7c785d03ccfbd to your computer and use it in GitHub Desktop.
Python: Merge two lists recursivley alternating between the heads of each list
def merge_lists_into_string(a, b, string):
'''
Takes two lists of varying size and merges them like so:
([1,2,3], [10, 20, 30, 40, 50]) >> [1, 10, 2, 20, 3, 30, 40, 50]
'''
if a:
string += a.pop(0)
if b:
string += b.pop(0)
if a or b:
string = merge_lists_into_string(a, b, string)
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment