Print a List in Columns in Python

In this article, we will discuss how to print a list in columns in Python.

Table Of Contents

Introduction

Suppose we have a list of 12 elements,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23, 33, 44]

Instead of printing the list elements one by one, we want to print the list content in columns like there will be 3 columns and 4 rows. Each row will have 3 elements. Like,

  • First row will have element from index zero to 2.
  • Second row will have elements from index 3 to 5.
  • Third row will have an elements from index 6 to 8.
  • Last row will have elements from index 9 to 11.

After printing this list into columns it will look like this,

Advertisements
11        78        86
19        32        26
20        21        21
23        33        44

There are different ways to do this. Let’s discuss them one by one.

Method 1: Using zip() function

In this technique, we will split our list into 3 parts. Each part will contain 1/3 of the list elements. Like in this case, we have 12 elements in the list. So first part will contain elements from index zero till 3, and second part will contain elements from index 4 to 7, and 3rd part will contain elements from index 8 to 11.

We will zip these 3 list slices together, and then we will iterate our this zipped object. The ith element of this zipped object will contain the ith element of the zipped lists. For example, first element of the zipped list contains the first element of each of the list. Similarly, the second element of the zipped object contains the second element of the zipped lists. We will iterate over all the elements of the zipped object, and print them. Let us see the complete example

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23, 33, 44]

cols = 3

# split list into three parts, and zip them together
zippedObj = zip(listOfNumbers[::cols], listOfNumbers[1::cols], listOfNumbers[2::cols])

# Iterate over zipped object.
for col1, col2, col3 in zippedObj:
    print(f'{col1: <10}{col2: <10}{col3}')

Output:

11        78        86
19        32        26
20        21        21
23        33        44

Our list had 12 elements, and we printed them in three columns. Therefore each row has 3 entries and there are total 4 rows.

Method 2: Using for loop

In this approach, first we will fetch the length of the list and then we will divide it with the number of columns. It will give us the number of rows. Then we will iterate from index zero till the number of rows, and for each row we will select the elements from the list and print them on the console.

For example, for the first row we will fetch elements from index zero till two. Basically first 3 elements and for the second row we will select next 3 elements. Basically we have calculated a formula to fetch the elements for the ith row, and we will use that in a for loop to fetch elements for each row and print them on the console.

This way we will print the list contents as columns. Let’s see the complete example.

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23, 33, 44]

# number of columns to be printed
cols = 3

# get length of list
len = len(listOfNumbers)

# get number of rows to be printed
rows = int(len/cols)

# iterate over all row numbers
for i in range(rows):
    # fetch content of each row, and print it
    sublist = listOfNumbers[i*cols : i*cols + cols]
    print(*sublist)

Output:

11 78 86
19 32 26
20 21 21
23 33 44

We printed the 12 list elements in 3 columns.

Summary

We learned about two different ways to print list elements as columns in Python.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top