[Python-ideas] Have a "j" format option for lists

Ken Kundert python-ideas at shalmirane.com
Wed May 9 21:22:56 EDT 2018

    I think this is the beginning of a great feature. And it fills a hole in the 
current string formatting. Specifically, we can carefully control the formatting 
of the base data types, but not collections.

I would like to see you flesh out the idea. In particular, I'd like to see you 
address cases where:
1. The underlying members in the collection are not strings. Besides the basic 
   types such as numbers, it would also be nice to be able to apply formats 
   recursively so that one can construct a string using the attributes of 
   members that are objects or items or other collections.
2. The ability to handle collections other than simple lists or iterables, such 
   as dictionaries.


On Wed, May 09, 2018 at 09:39:08AM -0300, Facundo Batista wrote:
> This way, I could do:
> >>> authors = ["John", "Mary", "Estela"]
> >>> "Authors: {:, j}".format(authors)
> 'Authors: John, Mary, Estela'
> In this case the join can be made in the format yes, but this proposal
> would be very useful when the info to format comes inside a structure
> together with other stuff, like...
> >>> info = {
> ...   'title': "A book",
> ...   'price': Decimal("2.34"),
> ...   'authors: ["John", "Mary", "Estela"],
> ... }
> ...
> >>> print("{title!r} (${price}) by {authors:, j}".format(**info))
> "A book" ($2.34) by John, Mary, Estela
> What do you think?
> -- .    Facundo

More information about the Python-ideas mailing list