Print Python Lists in Columns
Better utilise the space in your console when printing Python Iterables
Introduction
When we use the programming language Python, it is not as easy to output our lists in a column format. This is because Python does not have a built-in way to output lists in a column format. The two most common ways to output your lists in Python are the print()
and pprint()
functions. However, these functions do not provide a way to output your lists in a convenient and easy to read column format.
In this article, we will discuss how to output your Python lists as columns, and how to use the list_columns()
function in the toolbox_python
package to do this. This function is very useful when you have a long list of strings that you want to output in a more readable format. It is very easy to use, and provides a lot of customisation options to suit your needs.
How does it look in R?
When we use the programming language R, it is very easy to output our lists in a column format. This is very useful when you have a long list of strings that you want to output in a more readable format. Here is an example of how to output a list in R:
> my_list <- c('apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape')
> str(my_list)
[1] "apple" "banana" "cherry" "date" "elderberry"
[6] "fig" "grape"
This output is very easy to read, and is very useful when you have a long list of strings. For developers coming from R to Python, they may miss this formatting.
Existing Processes in Python
When we use the programming language Python, it is not as easy to output our lists in a column format. This is because Python does not have a built-in way to output lists in a column format.
The `print()` function
The print()
function is the most basic way to output your lists in Python. It is very simple to use and is the default way to output your lists. However, it does not provide a way to output your lists in a column format.
>>> my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
>>> print(my_list)
['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
This output is fine, and it works well. However, if you have a long list, it can be difficult to read. For example, let’s say you have a list of 100 items:
>>> my_list = list(range(100))
>>> print(my_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 98, 99]
(To save space in this article, the ...
is added by me; but Python will print the whole thing).
This is useful, but does not allow you to print your data in a column format.
The pprint() function
The pprint()
function is a built-in Python module that provides a way to pretty-print your lists. It is very useful when you have a long list and you want to output it in a more readable format.
>>> from pprint import pprint
>>> my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
>>> pprint(my_list)
['apple',
'banana',
'cherry',
'date',
'elderberry',
'fig',
'grape']
This output is much more readable than the print()
function. However, if you have a long list, it can still be difficult to read. For example, let’s say you have a list of 100 items:
>>> my_list = list(range(100))
>>> pprint(my_list)
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
...
98,
99]
(To save space in this article, the ...
is added by me; but Python will print the whole thing).
It does print your list in a single column, but this is not very useful when you have a long list, as there is lots of wasted white space.
New `list_columns()` function
There is a very convenient function in the toolbox_python
package called list_columns()
that allows you to print your lists as columns. This is very useful when you have a list of strings that you want to print in a column format.
Python Toolbox
Helper files/functions/classes for generic Python processes
data-science-extensions.com
You can install the package using pip
:
pip install toolbox_python
And then import it into your module:
>>> from toolbox_python.output import list_columns
Here is an example of how to use the list_columns()
function:
>>> my_list = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
>>> print(list_columns(my_list, print_output=False))
apple cherry elderberry grape
banana date fig
You’ll notice here that:
- The default output is in 4 columns. It’d be nice if it were optimised to print the number of columns based on the width of your screen; but it is not that advance.
- The
print_output=False
argument is used to return the output as a string, rather than printing it to the console. This is useful if you want to use the output in a different way. Here, we have turned off theprint_output
argument, because we are parse’ing the output of the function to a newprint()
operation. - The default number of spaces between each columns is 4 spaces.
The power of this function really begins to shine when we are dealing with large lists. For example, let’s say you have a list of 100 items:
>>> my_list = list(range(100))
>>> print(list_columns(my_list, print_output=False))
0 25 50 75
1 26 51 76
2 27 52 77
3 28 53 78
4 29 54 79
5 30 55 80
6 31 56 81
7 32 57 82
8 33 58 83
9 34 59 84
10 35 60 85
11 36 61 86
12 37 62 87
13 38 63 88
14 39 64 89
15 40 65 90
16 41 66 91
17 42 67 92
18 43 68 93
19 44 69 94
20 45 70 95
21 46 71 96
22 47 72 97
23 48 73 98
24 49 74 99
This output is a lot easier to read, and is much more useful than the print()
or pprint()
functions.
There are a couple of different ways in which we can customise the output of the list_columns()
function.
1. We can change the number of columns utilised:
>>> print(list_columns(list(range(100)), print_output=False, cols_wide=8))
0 13 26 39 52 65 78 91
1 14 27 40 53 66 79 92
2 15 28 41 54 67 80 93
3 16 29 42 55 68 81 94
4 17 30 43 56 69 82 95
5 18 31 44 57 70 83 96
6 19 32 45 58 71 84 97
7 20 33 46 59 72 85 98
8 21 34 47 60 73 86 99
9 22 35 48 61 74 87
10 23 36 49 62 75 88
11 24 37 50 63 76 89
12 25 38 51 64 77 90
2. We can change the gap between columns:
>>> print(list_columns(list(range(100)), print_output=False, cols_wide=8, gap=8))
0 13 26 39 52 65 78 91
1 14 27 40 53 66 79 92
2 15 28 41 54 67 80 93
3 16 29 42 55 68 81 94
4 17 30 43 56 69 82 95
5 18 31 44 57 70 83 96
6 19 32 45 58 71 84 97
7 20 33 46 59 72 85 98
8 21 34 47 60 73 86 99
9 22 35 48 61 74 87
10 23 36 49 62 75 88
11 24 37 50 63 76 89
12 25 38 51 64 77 90
3. We can print rowwise instead of the default colwise:
>>> print(list_columns(list(range(100)), print_output=False, cols_wide=8, columnwise=False))
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95
96 97 98 99
4. We are not just limited to list
objects; we can use set
, tuple
, and generators
too:
>>> print(
... "list:",
... list_columns(list(range(50)), print_output=False, cols_wide=8),
... sep="\n",
... )
list:
0 7 14 21 28 35 42 49
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
>>> print(
... "tuple:",
... list_columns(tuple(range(50)), print_output=False, cols_wide=8),
... sep="\n",
... )
tuple:
0 7 14 21 28 35 42 49
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
>>> print(
... "set:",
... list_columns(set(range(50)), print_output=False, cols_wide=8),
... sep="\n",
... )
set:
0 7 14 21 28 35 42 49
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
>>> print(
... "generator:",
... list_columns((val for val in range(50)), print_output=False, cols_wide=8),
... sep="\n",
... )
generator:
0 7 14 21 28 35 42 49
1 8 15 22 29 36 43
2 9 16 23 30 37 44
3 10 17 24 31 38 45
4 11 18 25 32 39 46
5 12 19 26 33 40 47
6 13 20 27 34 41 48
Conclusion
This list_columns()
function is a very useful tool to have in your Python toolbox. It allows you to output your lists in a column format, which is very useful when you have a long list of strings that you want to output in a more readable format. It is very easy to use, and provides a lot of customisation options to suit your needs, including the number of columns, the gap between columns, and whether to print rowwise or colwise. Be sure to use it in your next Python project!