0

I want to print a list in this way:

mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
[print(n) for n in mylist]

the reason to do it because I want to view all items in a vertical way, like the way to print things in for loop.

I used to do it alot with pycharm, no problem with it.

but when it comes to google colab.

it prints me something extra:

1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
[None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None,
 None]

how can I avoid, or remove those list of None in google colab.

Kai
  • 77
  • 1
  • 13
  • 5
    Don't use a list comprehension when you don't actually want to create a new list. Did you mean: `for n in mylist: print(n)`? – quamrana Feb 08 '21 at 09:08
  • 3
    That's because you are storing the return value of `print` that is `None` and storing it inside list (as part of list comprehension). You do not need list comprehension here. simply use explicit for loop, and print the values – Moinuddin Quadri Feb 08 '21 at 09:09
  • I know what you means, but I still like to print it with list comprehension because I always need to have many print with long lists, and I enjoy it is done in 1-line-code, unlike for-loop which is 2 lines. So I ready want to know a way to disable the return on those list of None, and print my list only. – Kai Feb 08 '21 at 09:13
  • You should not use a *list comprehension* if you do not need to store any value to a list. Using *list comprehension* for printing is missuse of comprehension statement – Moinuddin Quadri Feb 08 '21 at 09:16
  • Does this answer your question? [Is it Pythonic to use list comprehensions for just side effects?](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects) – Tomerikoo Feb 08 '21 at 09:20
  • @Kai you really shouldn't use list-comprehensions for side-effects – juanpa.arrivillaga Feb 08 '21 at 09:30

3 Answers3

2

You can use pprint to achieve pretty printing

from pprint import pprint

lis = list(range(1000))
pprint(lis)
JL0PD
  • 3,698
  • 2
  • 15
  • 23
  • I just your way, the print result go vertical down. But when I apply to pprint(mylist) it behave different. – Kai Feb 08 '21 at 10:17
  • @Kai it tries to fit best into terminal. If there's low amount of data, than it will print in single line, if amount is big, than if will print vertically. If it's matrix, than matrix will be print as matrix `pprint([list(range(10) for i in range(10)])` – JL0PD Feb 08 '21 at 10:51
  • ok, I never try it, I think it is good, and thank for suggesting it. – Kai Feb 08 '21 at 11:06
1

You probably don't want to use a list comprehension for printing things out; it works, but it's confusing. A for loop will be clearer:

mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
for n in mylist:
    print(n)

As far as what's happening in google colab, when you write [print(n) for n in mylist] it creates a list of all the results of the print() statements, which are all None. Google colab then prints the result of the last statement.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • ok thank, then I use for loop in one line. – Kai Feb 08 '21 at 09:24
  • @Kai why that one extra line bothers you so much? You're obsessing on the wrong things here... Please just keep in mind that a longer, ***more readable*** code, is much (much) better than a complex one-liner – Tomerikoo Feb 08 '21 at 09:50
  • I have been writing something very long codes, and also involves printing a lot of iterable objects. If printing only few of them I have no problem using for loop. but with many of them, it make my code so busy, and I find myself easier to recognize the print method from the list comprehension, and the overall codes look so clear to me (imagine looking the codes like the way look at a pic). – Kai Feb 08 '21 at 10:38
  • I say one more example if I continuely print 10 iterables, I like to put 10 lines of list comprehension together. and when I read this, this is alot more simple and understandable. If I make 10 for-loop together, I can see what ti is, but spend more time to read, and it look alot more busy. – Kai Feb 08 '21 at 10:38
  • maybe I am doing something that is quite not so universal format to all people. however, I just find this way (printing a iteratable in list comprehension) make code clear. Doing it in one line, or two lines, if doing a lot of repeated task, it still means alot differences. – Kai Feb 08 '21 at 10:41
  • If you're doing it a lot of times, write a separate function for it? (If it's an object rather than a list, you can also give it a `__str__` method so you can print it out in one go.) – Jiří Baum Feb 09 '21 at 05:13
0

It seems, you don't want to use a for loop.

You can do the following:

mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
_ = [print(n) for n in mylist]
Lescurel
  • 10,749
  • 16
  • 39
  • 1
    why to even create a list if you do not need it ? – Moinuddin Quadri Feb 08 '21 at 09:36
  • so this way seem to create a list with print() but don't register in memory, am I correct? – Kai Feb 08 '21 at 10:13
  • It'll construct it in memory and store it in the variable `_`; you can avoid storing it, with something like `[print(n) for n in mylist] and None`, but that'll still construct the list and then destroy it. – Jiří Baum Feb 09 '21 at 06:45