List Comprehension: why is this a syntax error?

后端 未结 4 849
迷失自我
迷失自我 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:14

    print in python 3 makes it more obvious on how to use it.

    the square brackets in the list comprehension denotes that the output will actually be a list. L1=['a','ab','abc'] print([item for item in L1]) This should do the trick.

提交回复
热议问题