diff --git a/tabulate/__init__.py b/tabulate/__init__.py index e100c09..13fe5fa 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2262,7 +2262,10 @@ def tabulate( ) if maxheadercolwidths is not None: - num_cols = len(list_of_lists[0]) + if len(list_of_lists): + num_cols = len(list_of_lists[0]) + else: + num_cols = 0 if isinstance(maxheadercolwidths, int): # Expand scalar for all columns maxheadercolwidths = _expand_iterable( maxheadercolwidths, num_cols, maxheadercolwidths diff --git a/test/test_regression.py b/test/test_regression.py index bf26247..39ee076 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -515,6 +515,12 @@ def test_exception_on_empty_data_with_maxcolwidths(): assert_equal(result, "") +def test_exception_on_empty_data_with_maxheadercolwidths(): + "Regression: exception on empty data when using maxheadercolwidths (github issue #365)" + result = tabulate([], maxheadercolwidths=5) + assert_equal(result, "") + + def test_numpy_int64_as_integer(): "Regression: format numpy.int64 as integer (github issue #18)" try: