List Comprehension: why is this a syntax error?

后端 未结 4 850
迷失自我
迷失自我 2020-12-08 18:39

Why is print(x) here not valid (SyntaxError) in the following list-comprehension?

my_list=[1,2,3]
[print(my_item) for my_item in my         


        
4条回答
  •  感情败类
    2020-12-08 19:27

    list comprehension are designed to create a list. So using print inside it will give an error no-matter we use print() or print in 2.7 or 3.x. The code

    [my_item for my_item in my_list] 
    

    makes a new object of type list.

    print [my_item for my_item in my_list]
    

    prints out this new list as a whole

    refer : here

提交回复
热议问题