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

前端 未结 4 938
醉话见心
醉话见心 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:23

    def union(a,b):
        a.extend(b)
        for item in a:
            print item,
        return a
    
    
    a=[1,2,3,4]
    b=[4,5,6]
    union(a,b)
    

    prints

    1 2 3 4 4 5 6
    

提交回复
热议问题