39

The IPython console prints a list of elements with line breaks so that each element is displayed in its own line. This is usually a feature, but in my case it is a bug: I need to copy and paste long lists, so I need a compact representation. How can I achieve this?

2
  • 3
    If you print thelist, it will be shown in the standard Python format. Commented Sep 25, 2013 at 17:56
  • if you dont mind, you can convert the list into a numpy array / tensor. and config np.set_printoptions(edgeitems=5, precision=3, suppress=True, linewidth=160, threshold=50) torch.set_printoptions(edgeitems=5, precision=3, sci_mode=False, linewidth=160, threshold=50) Commented Jul 2 at 11:05

3 Answers 3

55

You can use %pprint command to turn on/off pprint feature:

In [1]: range(24)
Out[1]:
[0,
 1,
 2,
 ...
 21,
 22,
 23]

In [2]: %pprint
Pretty printing has been turned OFF

In [3]: range(24)
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

If you want to turn off pprint permanently, make a profile, and add c.PlainTextFormatter.pprint = False to the profile file.

Linux example:

$ ipython profile create
[ProfileCreate] Generating default config file: '.../ipython_config.py'
[ProfileCreate] Generating default config file: u'..../ipython_notebook_config.py'
$ echo 'c.PlainTextFormatter.pprint = False' >> ~/.ipython/profile_default/ipython_config.py
Sign up to request clarification or add additional context in comments.

Comments

5

Start ipython with --no-pprint option.

$ ipython --no-pprint
...
IPython 0.13.2 -- An enhanced Interactive Python.
...
In [1]: lis = ['a'*10]*10

In [2]: lis
Out[2]: ['aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa']

Another options is to start ipython with --classic option, in that pprint is already disabled:

$ ipython --classic
...
IPython 0.13.2 -- An enhanced Interactive Python.
...
>>> lis = ['a'*10]*10
>>> lis
['aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa', 'aaaaaaaaaa']

1 Comment

BTW, IPython 1.1.0 is out, I've had no problems at all with it so far...
4

An alternative to turning off pretty printing entirely is to increase the max_width trait for the PlainTextFormatter.

Add the following to ipython_config.py (find it by ipython locate profile):

c.PlainTextFormatter.max_width = 120

which will allow the pretty printer to use less vertical space by allowing lines to extend out to 120 characters rather than the default of 79.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.