[Tutor] Lists+sorting

Pooja Bhalode poojabhalode11 at gmail.com
Mon Jan 4 11:56:00 EST 2016


Hi,
I wanted to check if I can write the following program in this manner as
well.

The problem is to merge the lists together and sort them.The solution they
have given is:

def linear_merge(list1, list2):


result = []

while len(list1) and len(list2):

if list1[0] < list2[0]:

result.append(list1.pop(0))

else:

result.append(list2.pop(0))


# Now tack on what's left

result.extend(list1)

result.extend(list2)

return result


So, is it correct if I write the function in this manner? This seems to
give me the correct solution though


def linear_merge(list1, list2):

for num in list2:

list1.append(num)

list1.sort()

# +++your code here+++

return list1


Thank you.


More information about the Tutor mailing list