Printing Lists As Tabular Data In PythonIntroductionIn the realm of working with data, Python is an excellent tool. With its wide array of libraries and frameworks, Python provides users with a huge collection of tools for working splendidly with data. A widespread activity in programming is the rendition of lists into tabular data. Whatever you may be working with, numbers, strings, or a mixture of both, Python offers several methods to make lists in a commendable form and print them in an organized way. In this detailed guide, we will study various methods that could help accomplish this, including plain text tables, PrettyTable, tabulate and pandas. In this tutorial, we will see the various examples for printing lists as tabular data in Python. 1. Plain text tablesWhen simplicity and degree of customization are the main factors, making plain text tables is a simple way of doing it. Python's built-in string formatting abilities allow you to tabularize data easily. Using a mix of strings, loops and formatting you can print lists in a narrow and easily-readable form. Let us see the code implementation below: Code Implementation: Output: + --------------- + --------------- + --------------- + | Column 1 | Column 2 | Column 3 | + --------------- + --------------- + --------------- + | 1 | Apple | 5 | + --------------- + --------------- + --------------- + | 2 | Banana | 8 | + --------------- + --------------- + --------------- + | 3 | Orange | 3 | + --------------- + --------------- + --------------- + Explanation:
2. PrettyTable:The PrettyTable library streamlines the process of making tables which is visually appealing using text. It presents a convenient API for making tables using a wide range of formatting possibilities. To use PrettyTable, you need to install it first: Now, let's explore how to use PrettyTable to print lists as tabular data: All the items will be returned by calling of {functionName}. Code Implementation: Output: +----------+----------+----------+ | Column 1 | Column 2 | Column 3 | +----------+----------+----------+ | 1 | BMW | 5 | | 2 | Mercedes | 8 | | 3 | Ferrari | 3 | +----------+----------+----------+ Explanation:
3. Tabulate:The tabulate library is the other powerful module for tabular data formatting as well. It supports different types of input data, among which are lists, dictionaries and pandas DataFrames. To use tabulate, you can install it with the following command: Now, let's see how to use tabulate to print lists as tabular data. Code Implementation: Output: ╒════════════╤════════════╤════════════╕ │ Column 1 │ Column 2 │ Column 3 │ ╞════════════╪════════════╪════════════╡ │ 1 │ Rose │ 5 │ ├────────────┼────────────┼────────────┤ │ 2 │ Jasmine │ 8 │ ├────────────┼────────────┼────────────┤ │ 3 │ Lotus │ 3 │ ╘════════════╧════════════╧════════════╛ Explanation:
4. Pandas:When it comes to advanced data manipulation and investigation, particularly in dealing with large data, Pandas Library is the best choice. Pandas DataFrame is a 2-dimensional labeled data structure that 'Pandas' offer. It can be used to easily manage and to transform tabular data. To use pandas, you can install it with the following command: Now, let's explore how to print lists as tabular data using 'pandas': Code Implementation: Output: Column 1 Column 2 Column 3 0 1 Apple 5 1 2 Banana 8 2 3 Orange 3 Explanation:
ConclusionIn the present article, we have looked at the various methods to print lists as tables in Python. Starting from simple plain text tables to pretty elegant ones like PrettyTable, tabulate, and pandas, you can choose your tools based on your needs and preferences. The suitable method to be selected is determined by aspects like the structure of your data, required customization of representation, and the need for additional features to do manipulation and analysis. Whether you just started with the programming or a seasoned developer already, learning these techniques will broaden your skill to show and work around with tabular data in Python. Next TopicProgramming paradigms in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India