Skip to content

Fix headers for list of dicts #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

jhadida
Copy link

@jhadida jhadida commented Feb 4, 2023

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

@astanin
Copy link
Owner

astanin commented Jul 23, 2025

I can understand your perspective, but this change will change the behavior of the library that some users may rely on.

The desired output can be achieved by a relatively basic pre-processing of the input data.

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

Successfully merging this pull request may close these issues.

2 participants