[PYTHON] I want to print in a comprehension

Confirmation of inclusion notation

When you want to check the inside of the for sentence, it is a little annoying to write it in the inclusion notation. In the first place, print cannot be written in the comprehension.

terminal.


$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num_list = [1,2,3,4,5]
>>> [print(i) for i in num_list]
  File "<stdin>", line 1
    [print(i) for i in num_list]
         ^
SyntaxError: invalid syntax

I get an error like this. Since print is not a function in the first place, it cannot be written without comprehension. So

terminal.


>>> def puts(i):
...     print i
...     return i
... 
>>> [puts(i) for i in num_list]
1
2
3
4
5
[1, 2, 3, 4, 5]

If you create a function like this, you will be able to print and generate a list.

python3

In python3, print is a function.

There are some changes in python2 to 3 series. One of them is that print is a function.

python3.


$ python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!
>>> print "Hello World!"
  File "<stdin>", line 1
    print "Hello World!"
                       ^
SyntaxError: Missing parentheses in call to 'print'
>>> num_list = [1,2,3,4,5]
>>> [print(i) for i in num_list]
1
2
3
4
5
[None, None, None, None, None]

Looking at the above, print is a function, so if you do not enclose it in (), you will get an error. Also, even if you write in the comprehension notation, no error will occur.

reference

If you are interested, please see the URL below for the differences between 2 and 3 in an easy-to-understand manner. http://postd.cc/the-key-differences-between-python-2-7-x-and-python-3-x-with-examples/

Recommended Posts

I want to print in a comprehension
I want to create a window in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to make input () a nice complement in python
I want to build a Python environment
I want to embed Matplotlib in PySimpleGUI
I want to create a pipfile and reflect it in docker
I want to make matplotlib a dark theme
I want to do Dunnett's test in Python
I want to easily create a Noise Model
I want to pin Datetime.now in Django tests
I want to make a game with Python
I don't want to take a coding test
I want to store DB information in list
I want to merge nested dicts in Python
I want to create a plug-in type implementation
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to display the progress in Python!
I want to upload a Django app to heroku
(Matplotlib) I want to draw a graph with a size specified in pixels
I want to convert a table converted to PDF in Python back to CSV
I want to sort a list in the order of other lists
I want to color a part of an Excel string in Python
I want to do a monkey patch only partially safely in Python
I want to write in Python! (1) Code format check
I want a mox generator
I want to iterate a Python generator many times
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to climb a mountain with reinforcement learning
I want to create a priority queue that can be updated in Python (2.7)
Even in JavaScript, I want to see Python `range ()`!
I made a script to put a snippet in README.md
I tried to implement a pseudo pachislot in Python
I want to find a popular package on PyPi
I want to solve Sudoku (Sudoku)
I want to easily build a model-based development environment
I want to set a life cycle in the task definition of ECS
I want a mox generator (2)
I want to split a character string with hiragana
I want to install a package of Php Redis
I want to see a list of WebDAV files in the Requests module
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
I want to manually create a legend with matplotlib
I want to send a business start email automatically
When you want to plt.save in a for statement
Python program is slow! I want to speed up! In such a case ...
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!