Ancestors

Written by Alex Willmer on 2024-12-17 at 14:09

[#]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

Written by Peter Drake on 2024-12-17 at 14:41

@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

Toot

Written by Alex Willmer on 2024-12-17 at 15:17

@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

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113668828077772367
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
259.412816 milliseconds
Gemini-to-HTML Time
0.708735 milliseconds

This content has been proxied by September (ba2dc).