Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down