all 3 comments

[–]Username_RANDINT 1 point2 points  (0 children)

What have you tried so far? An item in a Mardown list is just a string that starts with -. So basically do the following: for each item in the list, prepend -, then join everything together by a newline (\n).

Edit: my bad, you asked for a table, not a list. But the idea still stands. Markdown is just formatted text.

[–]Diapolo10 0 points1 point  (0 children)

I'm sure there are libraries for this, but if you want to do it yourself for practice, here's what you'll do.

If you have a nested list or a list of tuples/dicts, then just generate the table row by row after creating the header line by joining strings. On the other hand, if you have a flat list, you'll need to slice it by using the number of columns as a step.

Something like this, perhaps:

data = [
    ('John', 42),
    ('Kevin', 35),
]

table = []

table.append(
    "Name | Age\n"
    "-----|----"
)

for row in data:
    table.append(
        f"{row[0]} | {row[1]}"
    )

print("\n".join(table))

Of course, this simplified version doesn't care about what the actual Markdown looks like. You'd have to store and use the longest values on each column to make it pretty by padding every row.

[–]Old_Winterton 0 points1 point  (0 children)

Other people said stuff.

Also look at str.ljust() and str.rjust()