Created
January 3, 2014 23:45
-
-
Save rpappalax/8249003 to your computer and use it in GitHub Desktop.
Merge two sorted lists: l1, l2
http://stackoverflow.com/questions/464342/combining-two-sorted-lists-in-python
This file contains 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
#!/usr/bin/env python | |
## merge.py -- Merge two sorted lists -*- Python -*- | |
## Time-stamp: "2009-01-21 14:02:57 ghoseb" | |
l1 = [1, 3, 4, 7] | |
l2 = [0, 2, 5, 6, 8, 9] | |
def merge_sorted_lists(l1, l2): | |
"""Merge sort two sorted lists | |
Arguments: | |
- `l1`: First sorted list | |
- `l2`: Second sorted list | |
""" | |
sorted_list = [] | |
# Copy both the args to make sure the original lists are not | |
# modified | |
l1 = l1[:] | |
l2 = l2[:] | |
while (l1 and l2): | |
if (l1[0] <= l2[0]): # Compare both heads | |
item = l1.pop(0) # Pop from the head | |
sorted_list.append(item) | |
else: | |
item = l2.pop(0) | |
sorted_list.append(item) | |
# Add the remaining of the lists | |
sorted_list.extend(l1 if l1 else l2) | |
return sorted_list | |
if __name__ == '__main__': | |
print merge_sorted_lists(l1, l2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment