Skip to content

Fix headers for list of dicts - #10

Open
jhadida wants to merge 2 commits into
gregbanks:masterfrom
jhadida:master
Open

Fix headers for list of dicts#10
jhadida wants to merge 2 commits into
gregbanks:masterfrom
jhadida:master

Conversation

@jhadida

@jhadida jhadida commented Dec 5, 2022

Copy link
Copy Markdown

This contribution corrects the behavior of the headers option when the input data is a list of dict.

  1. Fix the behavior of headers="firstrow". The example below illustrates the current behavior, which is incorrect:
x = [ {'b': 'B', 'a': 'A'}, 
      {'a': 1, 'b': 2}, 
      {'a': 3, 'b': 4, 'c': 5} ]

print(tabulate( x, headers='firstrow' ))

# Current output:
#
#   B    A    c
# ---  ---  ---
#   2    1
#   4    3    5

# Proposed output:
#
#   B    A
# ---  ---
#   2    1
#   4    3
  1. Fix the current behavior when headers is specified as a list of keys:
x = [ {'a': 1, 'b': 2}, 
      {'a': 3, 'b': 4, 'c': 5} ]

print(tabulate( x, headers=['b','a'] ))

# Current output:
#
#       b    a
# --  ---  ---
#  1    2
#  3    4    5

# Proposed output:
#
#   b    a
# ---  ---
#   2    1
#   4    3
@smichr

smichr commented May 2, 2023

Copy link
Copy Markdown

Both look good to me: since dictionaries are order in recent python versions, the order of the headers is explicit. It also seems like a good idea to be able to request a subset from a set of data as you show.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants