Write a program that prints an associative array (key-value) to a table.


  • Standard loopholes are forbidden
  • Please include a link to an online interpreter and the byte count in the answer
  • Use box drawing characters to draw the table.
  • You can write both a full program or a function.
  • Key or value can't contain multiline strings.
  • Value/key can't be an array/associative array ( example: ["a"=>["a"=>"b"]] or ["a"=>["a","b"]] )

Special cases

  • If the programming language lacks of associative array , you can use a list of pairs. Example: [[key1,value1],[key2,value2]]
  • You can insert horizontal lines between successive pairs.
  • An input array can't be empty

This is a code-golf, the answer with the shortest byte count wins!


Your code must draw a vertical table like in the example from a associative array . The key must be at left and value must be at right and them needs to be separed,you can choose how separe them. Every key-value pair must be in another row. There must be only 2 columns for the keys and value.






closed as unclear what you're asking by xnor, caird coinheringaahing, Mr. Xcoder, Wheat Wizard, Jo King Aug 11 at 21:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ Thanks, please edit the post accordingly (we prefer not to have spec in comments). Also what are the rules regarding justification? (e.g ["abba"=>"1","b"=>"20"]) \$\endgroup\$ – Jonathan Allan Aug 11 at 16:24
  • \$\begingroup\$ Can we use any normal box drawing characters instead of the heavy ones? \$\endgroup\$ – Adám Aug 11 at 16:34
  • \$\begingroup\$ Can we use and when separating the columns? \$\endgroup\$ – Adám Aug 11 at 16:36
  • \$\begingroup\$ @PurpleP there is no specification about justification \$\endgroup\$ – Micio Informatico Aug 11 at 16:36
  • 4
    \$\begingroup\$ Maybe for next time, use the sandbox to get all the questions answered before posting? \$\endgroup\$ – Adám Aug 11 at 16:37

TryAPL, 1 byteSBCS

Anonymous tacit prefix function. As TryAPL has no associative arrays, this takes a list of pairs as argument.

Try APL!

 mix the list of pairs into a table

  • 6
    \$\begingroup\$ Question over, go home folks. \$\endgroup\$ – Purple P Aug 11 at 16:57

J, 1 byte


So… to take strings as input they need to be in boxes already, and to make them pairs I think they need to be put into a box again so that they can be treated separately. I'm not too sure about the second part.

Anyway, unboxing the whole thing gives the desired form.

I get a sense that this could be a 0 byte answer if we twist the format enough (or maybe less, since a 2d array of boxes seems to be the proper input format instead of my weird format), but I don't understand boxing all that well.

Try it online!


PHP, 563 bytes

    $t = ["a"=>"b","c"=>"d"];
    $k = $v = $i = $j = 0;
    foreach ($t as $a => $b){
        $k = (strlen($a) > $k ? strlen($a) : $k);
        $v = (strlen($b) > $v ? strlen($b) : $v);
    echo "┏"; for(; $i++ < $k + $v + 3;) echo "━";
    echo "┓\n";
    foreach ($t as $a => $b){
        echo "|{$a} ";
        for($i = 0; $i < $k - strlen($a); $i++) echo " ";
        echo "| {$b}";
        for($i = 0; $i < $v - strlen($b); $i++) echo " ";
        echo "|\n";
    echo "┗"; for(; $j++ < $k + $v + 3;) echo "━";
    echo "┛";

Try it online!


Charcoal, 62 bytes


Try it online! Link is to verbose version of code. Charcoal's code page doesn't include box-drawing characters so they cost 3 bytes but the code uses them instead of their three-byte encoding because it looks nicer. Takes input as an array of tuples as Charcoal doesn't have an iterable dictionary. Explanation:


Find the length of the longest key and value and repeat according to that length.


Embed the s in ┏┳┓.


For each tuple, pad the strings to the length of the s and wrap them with s.


Embed the s in ┗┻┛.


Python 3, 189 172 168 173 169 bytes

def f(d):t=d.items();a,b=map(lambda*l:max(map(len,l)),*t);c='─'*a,'─'*b;print("┌%s┬%s┐"%c,*[f"│{k:{a}}│{v:{b}}│"for k,v in t],"└%s┴%s┘"%c,sep="\n")

Try it online!


Not the answer you're looking for? Browse other questions tagged or ask your own question.