Last active
August 12, 2016 03:06
-
-
Save Prodge/191164c41ba54202c7c7c785d03ccfbd to your computer and use it in GitHub Desktop.
Python: Merge two lists recursivley alternating between the heads of each list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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