From 307c46e62e83e7e915832e766db121aefe0d9da6 Mon Sep 17 00:00:00 2001 From: Allinson Date: Thu, 5 Mar 2020 09:10:55 +0000 Subject: [PATCH 1/2] header order: unique items changed to dictionary order --- tabulate.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tabulate.py b/tabulate.py index 454d464b..5f955eee 100755 --- a/tabulate.py +++ b/tabulate.py @@ -1052,10 +1052,14 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"): uniq_keys.update(keys) rows = rows[1:] for row in rows: + list_pos = 0 for k in row.keys(): - # Save unique items in input order - if k not in uniq_keys: - keys.append(k) + # Save unique items in dictionary order + if k in uniq_keys: + list_pos = keys.index(k) + else: + list_pos += 1 + keys.insert(list_pos, k) uniq_keys.add(k) if headers == "keys": headers = keys From 668bfc1d7afe86a348215d0cefff440f16e5a330 Mon Sep 17 00:00:00 2001 From: Allinson Date: Thu, 5 Mar 2020 14:10:37 +0000 Subject: [PATCH 2/2] changing test input based on previous changes made --- test/test_input.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_input.py b/test/test_input.py index 5443ab96..ec16e590 100644 --- a/test/test_input.py +++ b/test/test_input.py @@ -407,11 +407,11 @@ def test_list_of_dicts_with_missing_keys(): lod = [{"foo": 1}, {"bar": 2}, {"foo": 4, "baz": 3}] expected = "\n".join( [ - " foo bar baz", + " foo baz bar", "----- ----- -----", " 1", - " 2", - " 4 3", + " 2", + " 4 3", ] ) result = tabulate(lod, headers="keys")