[#]TIL to use a dict-comprehension in an f-string add parens or whitespace #Python
d = {'a': 1, 'b': 2, 'c': 3, 'd': 5, 'e': 6}
f"{({k: v for k, v in d.items() if v % 2})}"
"{'a': 1, 'c': 3, 'd': 5}"
f"{ {k: v for k, v in d.items() if v % 2} }"
"{'a': 1, 'c': 3, 'd': 5}"
=> More informations about this toot | More toots from alex@moreati.org.uk
@alex Good to know.
Having whitespace matter feels un-Pythonic. Is there some other use for double curly braces?
=> More informations about this toot | More toots from peterdrake@mstdn.social
@peterdrake in an f-string double curly braces produce a single brace, e.g. f'{{foo}}' -> '{foo}'
=> More informations about this toot | More toots from alex@moreati.org.uk
text/gemini
This content has been proxied by September (ba2dc).