Alternate code on 17


#1


It asks to add the lists together but that .append isnt a good way to do it as it just adds the whole list at the end. But the code i came up with works, is there any reason not to do it that way.


m = [1, 2, 3]
n = [4, 5, 6]

# Add your code here!
def join_lists(x,y):
    for y in y:
        x.append(y)
    return x

print join_lists(m, n)
# You want this to print [1, 2, 3, 4, 5, 6]


#2

I wonder that why cannot use List extend() method
list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
https://developers.google.com/edu/python/lists

Maybe extend method only support by python3


#3

Hi,
I think extend() is supported in python 2 according to their documentation.
I have tried using extend in Codecademy as well and it worked.

m = [1, 2, 3]
n = [4, 5, 6]

# Add your code here!
def joint_lists(x, y):
    return x.extend(y)

print join_lists(m, n)

and I guest , another simplest way is just by adding them.

m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x, y):
    return x+y

print join_lists(m, n)

#4

That would be concatenation, but we get the drift.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.