-1

ex. if input "-1 1 3 -2 2", answer should be "-1 -2 1 3 2"

def specialsort(numbers):

    list1 = []
    list2 = []
    list = numbers.split(" ")

    for number in list:
        if int(number)<0:
            list1.append(number)
        else:
            list2.append(number)
    result = list1 + list2
    return " ".join(result)

My question is : In this source code above, when I first used extend list function like result = list1.extend(list2), it turned out to be an error saying TypeError: can only join an iterable. And when I edited it into "result = list1 + list2", it finally worked well. But I still don't understand why the one with extend function doesn't work while the other works well without problem because list1.extend(list2) returns same result with 'list1 + list2'.

If anyone knows about this problem, can you please explain about it? I would really appreciate your answer. Thanks :)

Gabio
  • 9,126
  • 3
  • 12
  • 32

1 Answers1

1

Your code:

result = list1 + list2

makes a new list labelled result, whereas your previous:

result = list1.extend(list2)

extends list1 with the contents of list2, so changing list1, but, I suspect the result label will end up with None.

quamrana
  • 37,849
  • 12
  • 53
  • 71