Adventures in Machine Learning

Mastering Column Printing in Python: Tips and Techniques

Printing a List in Columns in Python

Python is a powerful programming language that provides developers with a wide range of libraries and tools to make their work easy and efficient. In this article, we will explore different ways of printing a list in columns in Python.

Using the zip() function with formatted string literals

One way of printing a list in columns is by using the zip() function with formatted string literals. The zip() function is a built-in Python function that takes iterables as input and returns an iterator that aggregates elements from each of the iterables.

To use this method, we first define our list and divide it into two equal parts. We then zip the two lists together and use formatted string literals to print the columns.

Here is an example:

“`python

fruits = [‘Apple’, ‘Banana’, ‘Orange’, ‘Grape’, ‘Pineapple’, ‘Mango’]

first_column = fruits[:len(fruits)//2]

second_column = fruits[len(fruits)//2:]

for fruit1, fruit2 in zip(first_column, second_column):

print(f'{fruit1:<15}{fruit2}')

“`

In this example, we define our list ‘fruits’ and divide it into two equal parts using Python’s slicing notation. We then use the zip() function to combine the two parts into pairs, ‘fruit1’ and ‘fruit2’.

Finally, we use formatted string literals to print the two columns side by side.

Printing list in 2 columns

Another way of printing a list in columns is by creating a loop that iterates through the list and prints every other item in the next column. Here is an example:

“`python

fruits = [‘Apple’, ‘Banana’, ‘Orange’, ‘Grape’, ‘Pineapple’, ‘Mango’]

for i in range(0, len(fruits), 2):

print(f'{fruits[i]:<15}{fruits[i+1]}')

“`

In this example, we create a for loop that iterates through the list by steps of two.

We then use formatted string literals to print the two columns side by side.

Printing headers before columns

To make our columns more readable, we can add headers before each column. Here is an example:

“`python

fruits = [‘Apple’, ‘Banana’, ‘Orange’, ‘Grape’, ‘Pineapple’, ‘Mango’]

print(f'{“First Column”:<15}{"Second Column"}')

print(f'{“-“*15:<15}{"-"*15}')

for i in range(0, len(fruits), 2):

print(f'{fruits[i]:<15}{fruits[i+1]}')

“`

In this example, we print the headers before printing the columns.

We first print the header for the first column (which is “First Column”) and then the header for the second column (which is “Second Column”). We then use formatted string literals to print the two columns side by side.

Printing a List of Lists in columns in Python

Printing a list of lists in columns is slightly more complicated than printing a simple list. However, there are several libraries and modules in Python that make this task easy and efficient.

Using formatted string literals for headers and rows

One way of printing a list of lists in columns is by using formatted string literals for the headers and rows. Here is an example:

“`python

data = [[‘John’, ‘Doe’, 28], [‘Jane’, ‘Doe’, 32], [‘Alex’, ‘Smith’, 24]]

print(f'{“First Name”:<10}{"Last Name":<10}{"Age"}')

print(f'{“-“*10:<10}{"-"*10:<10}{"---"}')

for row in data:

first, last, age = row

print(f'{first:<10}{last:<10}{age}')

“`

In this example, we define our list of lists ‘data’ and print the headers before printing the rows.

We then iterate through the rows and use formatted string literals to print the rows in columns.

Using the tabulate module

Another way of printing a list of lists in columns is by using the tabulate module. The tabulate module is a third-party library that provides an easy way of pretty-printing tabular data in Python.

To use the tabulate module, we first need to install it using pip. Here is an example:

“`python

!pip install tabulate

“`

Once we have installed the tabulate module, we can use its tabulate() function to print our list of lists in columns.

Here is an example:

“`python

from tabulate import tabulate

data = [[‘John’, ‘Doe’, 28], [‘Jane’, ‘Doe’, 32], [‘Alex’, ‘Smith’, 24]]

print(tabulate(data, headers=[‘First Name’, ‘Last Name’, ‘Age’], tablefmt=’orgtbl’))

“`

In this example, we import the tabulate module and define our list of lists ‘data’. We then use the tabulate() function to print the rows in columns, with the headers provided as a list of strings and the table formatting specified as ‘orgtbl’.

Using the prettytable module

The prettytable module is another third-party library that provides an easy way of printing tabular data in Python. Here is an example:

“`python

from prettytable import PrettyTable

data = [[‘John’, ‘Doe’, 28], [‘Jane’, ‘Doe’, 32], [‘Alex’, ‘Smith’, 24]]

table = PrettyTable()

table.field_names = [‘First Name’, ‘Last Name’, ‘Age’]

for row in data:

table.add_row(row)

print(table)

“`

In this example, we import the prettytable module and define our list of lists ‘data’. We then create an instance of the PrettyTable class and set the field names for the headers.

Finally, we iterate through the rows and add them to the table using the add_row() method. We then print the table using the print() function.

Conclusion

Printing a list in columns or a list of lists in columns is a common task in Python programming. In this article, we have explored different ways of printing columns using built-in functions, third-party libraries, and modules in Python.

We hope that you have learned something new and useful that you can apply to your Python projects. In conclusion, printing columns of data is a common need in many Python projects, whether it’s a simple list or a complex list of lists.

This article has explored different ways of achieving this task using built-in functions, third-party libraries, and modules. By using techniques such as the zip() function with formatted string literals or modules such as tabulate and prettytable, you can quickly and easily output data in columns that is both readable and presentable.

Whether you’re a beginner or an experienced Python developer, knowing how to print your data in columns is a valuable tool to have in your arsenal.

Popular Posts