Skip to content

Instantly share code, notes, and snippets.

@blluv
Last active May 26, 2023 00:21
Show Gist options
  • Save blluv/f8374bbc3915724b8bb81768174747d1 to your computer and use it in GitHub Desktop.
Save blluv/f8374bbc3915724b8bb81768174747d1 to your computer and use it in GitHub Desktop.
def printAsTable(d: list, index=False):
headers = list(d[0].keys())
body = []
for row in d:
body.append(list(map(lambda v: row.get(v, ""), headers)))
if index:
headers = [""] + headers
body = list(map(lambda v: [v[0]] + v[1], enumerate(body)))
print(tabulate(body, headers, tablefmt="simple_grid"))
def printAsTable2(d: list, index=False):
headers = list(set(sum(map(lambda v: list(v.keys()), d), [])))
body = []
for row in d:
body.append(list(map(lambda v: row.get(v, ""), headers)))
if index:
headers = [""] + headers
body = list(map(lambda v: [v[0]] + v[1], enumerate(body)))
print(tabulate(body, headers, tablefmt="simple_grid"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment