Python: Why “return” won´t print out all list elements in a simple for loop and “print” will do it?

前端 未结 4 886
醉话见心
醉话见心 2021-01-15 05:10

Im trying to print out all elements in a list, in Python, after I´ve appended one list to another. The problem is that it only prints out every element when I use PRINT inst

4条回答
  •  不知归路
    2021-01-15 05:40

    return means end of a function. It will only return the first element of the list.

    For your print version, a.append(b) makes a = [1,2,3,4,[1,2,3]] so you will see the elements before None. And the function returns nothing, so print union(a, b) will print a None.

    I think you may want:

    def union(a, b):
        a.extend(b)
        return a
    

    Or

    def union(a, b):
        return a + b
    

提交回复
热议问题