How to Limit Rows When Using Python Tabulate

Tabulate is a powerful Python library for creating formatted tables.

Sometimes, you need to limit the number of rows displayed. This tutorial will show you various methods to do this.

 

 

Slice data before passing to tabulate

You can use Python’s built-in slicing to limit the rows before passing the data to tabulate.

To limit the number of rows to 5, you can use slicing like this:

from tabulate import tabulate
data = [
    ["Amr", 28, "Engineer"],
    ["Fatima", 35, "Doctor"],
    ["Hassan", 42, "Teacher"],
    ["Nour", 31, "Designer"],
    ["Yara", 39, "Lawyer"],
    ["Karim", 45, "Accountant"],
    ["Laila", 33, "Scientist"]
]
headers = ["Name", "Age", "Profession"]
limited_data = data[:5]
print(tabulate(limited_data, headers=headers, tablefmt="grid"))

Output:

+----------+-----+-------------+
| Name     | Age | Profession  |
+==========+=====+=============+
| Amr      |  28 | Engineer    |
+----------+-----+-------------+
| Fatima   |  35 | Doctor      |
+----------+-----+-------------+
| Hassan   |  42 | Teacher     |
+----------+-----+-------------+
| Nour     |  31 | Designer    |
+----------+-----+-------------+
| Yara     |  39 | Lawyer      |
+----------+-----+-------------+

This method slices the data list to include only the first 5 elements.

 

Using itertools.islice

For more control over the slicing, you can use itertools.islice to limit the rows.

To limit the rows to 3, starting from the second row, you can use itertools.islice like this:

from tabulate import tabulate
from itertools import islice
data = [
    ["Amr", 28, "Engineer"],
    ["Fatima", 35, "Doctor"],
    ["Hassan", 42, "Teacher"],
    ["Nour", 31, "Designer"],
    ["Yara", 39, "Lawyer"],
    ["Karim", 45, "Accountant"],
    ["Laila", 33, "Scientist"]
]
headers = ["Name", "Age", "Profession"]
limited_data = list(islice(data, 1, 4))
print(tabulate(limited_data, headers=headers, tablefmt="grid"))

Output:

+----------+-----+-------------+
| Name     | Age | Profession  |
+==========+=====+=============+
| Fatima   |  35 | Doctor      |
+----------+-----+-------------+
| Hassan   |  42 | Teacher     |
+----------+-----+-------------+
| Nour     |  31 | Designer    |
+----------+-----+-------------+

This method uses itertools.islice to select rows 2 to 4 from the data list.

 

List comprehension with enumeration

You can use list comprehension with enumeration to select specific rows based on their index.

To select every other row up to the 5th row, you can do it like this:

from tabulate import tabulate
data = [
    ["Amr", 28, "Engineer"],
    ["Fatima", 35, "Doctor"],
    ["Hassan", 42, "Teacher"],
    ["Nour", 31, "Designer"],
    ["Yara", 39, "Lawyer"],
    ["Karim", 45, "Accountant"],
    ["Laila", 33, "Scientist"]
]
headers = ["Name", "Age", "Profession"]
limited_data = [row for i, row in enumerate(data) if i % 2 == 0 and i < 5]
print(tabulate(limited_data, headers=headers, tablefmt="grid"))

Output:

+----------+-----+-------------+
| Name     | Age | Profession  |
+==========+=====+=============+
| Amr      |  28 | Engineer    |
+----------+-----+-------------+
| Hassan   |  42 | Teacher     |
+----------+-----+-------------+
| Yara     |  39 | Lawyer      |
+----------+-----+-------------+

This method uses list comprehension with enumeration to select every other row (even-indexed rows) up to the 5th row.

 

Filter rows based on criteria before tabulation

You can apply filtering conditions to select specific rows based on data content.

To filter rows where the age is greater than 35, you can use this approach:

from tabulate import tabulate
data = [
    ["Amr", 28, "Engineer"],
    ["Fatima", 35, "Doctor"],
    ["Hassan", 42, "Teacher"],
    ["Nour", 31, "Designer"],
    ["Yara", 39, "Lawyer"],
    ["Karim", 45, "Accountant"],
    ["Laila", 33, "Scientist"]
]
headers = ["Name", "Age", "Profession"]
filtered_data = [row for row in data if row[1] > 35]
print(tabulate(filtered_data, headers=headers, tablefmt="grid"))

Output:

+----------+-----+-------------+
| Name     | Age | Profession  |
+==========+=====+=============+
| Hassan   |  42 | Teacher     |
+----------+-----+-------------+
| Yara     |  39 | Lawyer      |
+----------+-----+-------------+
| Karim    |  45 | Accountant  |
+----------+-----+-------------+

This method filters the data based on a specific condition (age > 35) before passing it to tabulate.

Leave a Reply

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