# If I return a list from a function that is more than 26 elements, the list goes vertical

If I return a list from a function that is more than 26 elements, the list goes vertical instead of horizontal even if I turn pretty printing off. Is this normal, and can I turn it off? I'm just returning the list, not using print explicitly. i.e.

With range 26 in the testfunc it shows a horizontal list, but if I increase that to 27 it goes vertical:

def tester():
v=[]
for i in range(26):
v.append(3)
return v

edit retag close merge delete

This question was asked again a year later:

( 2018-06-06 07:17:48 +0100 )edit

Sort by ยป oldest newest most voted

sage does it for us. (And i really enjoy the output while printing e.g. some points on elliptic curves.)

But often space is expensive. In such cases i use my own explicit will (after seeing the mess..). For instance, from the sage interpreter opened in a linux console, here are some more or less equivalent ways:

sage: v = 30 * [3,]    # comma is not needed, but humanly clears, this is a list repeated 30x
sage: # v = [ 3 for _ in range(30) ]    # would be the list comprehension for the above
sage: repr(v)
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: print repr(v)
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: str(v)
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: print str(v)
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: print "v = %s" % v
v = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
sage: v.__str__()
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: v.__repr__()
'[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]'
sage: v

[3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3]


(The last is the mess, that we often want to avoid on the small laptop terminal in the train.)

more

There is a "pretty print" feature of ipython/jupyter, that can be switched off with the %pprint magic:

In [2]: tester()
Out[2]:
[3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3]

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

In [4]: tester()
Out[4]: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]


However, this was from a genuine ipython/jupyter console (loaunched from Sage with sage -ipython), not Sage's one, for which it does not work, so there might be a broken interaction with Sage's own pretty printing features.

This is now trac ticket 22968, thanks for reporting.

more