<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/printing-lists-ssthuybdi" -->

---
title: Printing Lists | daily.dev
description: A short technique note on printing comma-separated lists: instead of printing the separator after each element and trimming the trailing one, print the comma...
canonical: https://daily.dev/posts/printing-lists-ssthuybdi
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Printing Lists | daily.dev
og:description: A short technique note on printing comma-separated lists: instead of printing the separator after each element and trimming the trailing one, print the comma...
og:url: https://daily.dev/posts/printing-lists-ssthuybdi
og:image: https://api.daily.dev/og/posts/sSThUYbDI.png
og:image:alt: Printing Lists
og:image:width: 1200
og:image:height: 630
og:locale: en
---

[matklad](https://daily.dev/sources/matklad)

[Read post](https://api.daily.dev/r/sSThUYbDI)

# [Printing Lists](https://api.daily.dev/r/sSThUYbDI "Go to post")

A short technique note on printing comma-separated lists: instead of printing the separator after each element and trimming the trailing one, print the comma before each element except the first. This avoids special-casing the last item and results in cleaner code.

Aug 14•1m read time•From [matklad.github.io](https://api.daily.dev/r/sSThUYbDI "matklad.github.io")

[![Post cover image](https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008)](https://api.daily.dev/r/sSThUYbDI "Go to post")

Questions this post answers

What's a clean way to print a comma-separated list without a trailing comma?

Print the comma before each element instead of after it, skipping the comma only for the first element. This avoids needing to detect the last element or trim a trailing separator, resulting in simpler loop code that handles the common case of joining items with a delimiter. Developers refining everyday formatting idioms often browse similar coding tips on daily.dev.

73 Impressions

Comment

Bookmark

Copy

![Placeholder image for anonymous user](https://media.daily.dev/image/upload/s--qsFuKGv_--/t_logo,f_auto/public/noProfile)Share your thoughtsPost

[![matklad's image](https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/c34dbbf360354a179786e8a127718b12)](https://daily.dev/sources/matklad)

[matklad](https://daily.dev/sources/matklad "https://daily.dev/sources/matklad")

Matthias Noback's expertise in PHP and domain-driven design (DDD) offers readers insights into buil... Read more

33 Followers

•

474 Upvotes

#### Would you recommend this post?

Copy link

WhatsApp

Facebook

X

New Squad

Copy linkShare with your friends

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Printing Lists","url":"https://daily.dev/posts/printing-lists-ssthuybdi","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/printing-lists-ssthuybdi"},"datePublished":"2026-08-14T12:39:19.126Z","dateModified":"2026-08-14T12:39:33.203Z","description":"A short technique note on printing comma-separated lists: instead of printing the separator after each element and trimming the trailing one, print the comma...","image":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","thumbnailUrl":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","isAccessibleForFree":true,"articleSection":"matklad","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"matklad","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/c34dbbf360354a179786e8a127718b12","url":"https://daily.dev/sources/matklad"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/printing-lists-ssthuybdi","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"","timeRequired":"PT1M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"matklad","item":"https://daily.dev/sources/matklad"},{"@type":"ListItem","position":3,"name":"Printing Lists"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/printing-lists-ssthuybdi#faq","mainEntity":[{"@type":"Question","name":"What's a clean way to print a comma-separated list without a trailing comma?","acceptedAnswer":{"@type":"Answer","text":"Print the comma before each element instead of after it, skipping the comma only for the first element. This avoids needing to detect the last element or trim a trailing separator, resulting in simpler loop code that handles the common case of joining items with a delimiter. Developers refining everyday formatting idioms often browse similar coding tips on daily.dev."}}]}
```

