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.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.