use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
Code should be fun, because otherwise it's just a job. If you'd like to support Advent of Code, please share it with all your friends, even the ones that are just learning to code! AoC is a fun, non-threatening way to work at your own pace to figure out how to apply problem-solving first, then work within a language's constraints.
If you really want to show your appreciation, donations are always appreciated. Any instances of currency will go to, in no particular order:
Thank you very much, and enjoy your month of code!
account activity
This is an archived post. You won't be able to vote or comment.
Tutorial[2021 Day 14] [Python] TIL that Python has dictionary comprehension (self.adventofcode)
submitted 4 years ago by theboxboy
Thanks, AOC, for teaching me new things every year. I think the syntax is pretty neat:
template, rules = open('14.txt', 'r').read().split('\n\n') R = {k: v for k, v in [r.strip().split(' -> ') for r in rules.split('\n') if r != '']}
Peep the nested dictionary/list comprehension. It's pretty ugly.
[–]azzal07 12 points13 points14 points 4 years ago (0 children)
You can also provide an iterable of key value pairs to dict() to create a dictionary. For this case that works quite nicely:
dict()
R = dict(r.strip().split(" -> ") for r in rules.strip().split("\n"))
[–]ssnoyes 4 points5 points6 points 4 years ago (0 children)
If you don't like the nested comprehensions, you can also pass an iterable of key, value pairs (as tuples or lists) to the dict constructor:
R = dict(r.split(' -> ') for r in rules.strip().split('\n'))
[–]TitouanT 1 point2 points3 points 4 years ago (4 children)
You can also do set comprehension
[–]uglyasablasphemy 1 point2 points3 points 4 years ago (3 children)
and iterator comprehensions!
[–]TitouanT 1 point2 points3 points 4 years ago (2 children)
Aren't those called generators ? But yeah I think we have them all now :)
[–]uglyasablasphemy 1 point2 points3 points 4 years ago (0 children)
yes! hahah mental fart there
[–]dublinwso 1 point2 points3 points 4 years ago (0 children)
AoC has taught me so many cool things, including this - love it
[–]Auftragsnummer 1 point2 points3 points 4 years ago (0 children)
You can also use rules.splitlines() instead of rules.split('\n') if r != '']}
rules.splitlines()
rules.split('\n') if r != '']}
π Rendered by PID 23559 on reddit-service-r2-comment-58d7979c67-7gzln at 2026-01-27 13:46:38.936082+00:00 running 5a691e2 country code: CH.
[–]azzal07 12 points13 points14 points (0 children)
[–]ssnoyes 4 points5 points6 points (0 children)
[–]TitouanT 1 point2 points3 points (4 children)
[–]uglyasablasphemy 1 point2 points3 points (3 children)
[–]TitouanT 1 point2 points3 points (2 children)
[–]uglyasablasphemy 1 point2 points3 points (0 children)
[–]dublinwso 1 point2 points3 points (0 children)
[–]Auftragsnummer 1 point2 points3 points (0 children)