Printing Lists

matklad Tools

Summary

A concise Zig idiom for printing comma-separated lists by conditionally printing the comma before each item except the first.

<header> <h1>Printing Lists</h1> <time class="meta" datetime="2026-08-14">Aug 14, 2026</time> </header> <p>To print a comma-separated list, a concise idiom is to optionally print the comma first, before the element:</p> <figure class="code-block"> <pre><code><span class="line"><span class="hl-keyword">for</span> (items, <span class="hl-numbers">0</span>..) <span class="hl-operator">|</span>item, item_index<span class="hl-operator">|</span> {</span> <span class="line"> <span class="hl-keyword">if</span> (item_index &gt; <span class="hl-numbers">0</span>) std.debug.print(<span class="hl-string">&quot;, &quot;</span>);</span> <span class="line"> std.debug.print(<span class="hl-string">&quot;{}&quot;</span>, .{item})</span> <span class="line">}</span></code></pre> </figure>
Original Article
View Cached Full Text

Cached at: 08/14/26, 03:21 PM

# Printing Lists Source: [https://matklad.github.io/2026/08/14/printing-lists.html](https://matklad.github.io/2026/08/14/printing-lists.html) Aug 14, 2026To print a comma\-separated list, a concise idiom is to optionally print the comma first, before the element: ``` for (items, 0..) |item, item_index| { if (item_index > 0) std.debug.print(", "); std.debug.print("{}", .{item}) } ```

Similar Articles

Steering Zig Fmt

Lobsters Hottest

A blog post describing two tips for using `zig fmt` effectively, highlighting its 'steerable' formatting approach where trailing commas and line breaks control layout decisions, and showcasing columnar array formatting.

Zig by Example

Hacker News Top

A hands-on introduction to the Zig programming language via annotated examples, covering basic to advanced topics. Inspired by Go by Example.

Zig by Example

Lobsters Hottest

Zig by Example is a hands-on introduction to the Zig programming language, featuring annotated example programs for version 0.16.

Tagged Union Subsets with Comptime in Zig

Mitchell Hashimoto

Mitchell Hashimoto demonstrates how to use Zig's comptime to create subset types of tagged unions, enabling compile-time safety without exhaustive case handling.