Need some help refactoring list comprehension
Preface: I am an experienced programmer in multiple languages, but very much a beginner when it comes to Python as I just added it to my languages and the "Pythonic way" sometimes eludes me.
I have the following code as part of the solution for Day2 for AdventOfCode:
total_score_p1 = sum(scores_p1[x] for x in raw_data)
total_score_p2 = sum(scores_p2[x] for x in raw_data)
scores_p1andscores_p2are dictionariesraw_datais a list of string values - the keys
Is there any way to use only a single list comprehension and get the whole into a single line, like, e.g.
total_score_p1, total_score_p2 =
I know that I can just do:
total_score_p1, total_score_p2 = sum(scores_p1[x] for x in raw_data), sum(scores_p2[x] for x in raw_data)
But I want only a single iteration over raw_data and list comprehension
Originally, I had:
for x in raw_data:
total_score_p1 += scores_p1[x]
total_score_p2 += scores_p2[x]
Is what I want even possible? What am I not seeing? I guess my question is mostly philosophical as the data is only 2500 elements long.
My full code as of now for those who are interested:
scores_p1 = {"A X":4, "A Y":8, "B X":1, "A Z":3, "C X":7, "B Y":5, "B Z":9, "C Y":2, "C Z":6}
scores_p2 = {"A Y":4, "A Z":8 ,"B X":1, "A X":3, "C Z":7, "B Y":5, "B Z":9, "C X":2, "C Y":6}
raw_data = [x.strip() for x in open("Input_Day2.txt").read().split("\n")]
total_score_p1, total_score_p2 = sum(scores_p1[x] for x in raw_data), sum(scores_p2[x] for x in raw_data)
print(f"Part 01: {total_score_p1}\nPart 02: {total_score_p2}")
10 comments · 67% upvoted · other discussions
These are not list comprehensions, but generator expressions being fed to
sum. Frankly I don't see a clear path to further simplifying the code, although I guess you could use this if you really wanted a one-liner:Worth noting that I do prefer
as a more readable option.
desrtfx [OP] 1 points · Dec 2022 · but other languages pro
Thank you for your explanations!
Really appreciate that.
Also, thanks for the correction on list comprehension vs. generator expressions.
So, I wasn't so extremely far off with my original approach, then.
I just thought that it wasn't very Pythonic.
I am always in awe when I see the clever solutions for AdventOfCode puzzles and try to come up with my own, but with Python this has been a challenge so far (not really all too far in my learning).
Other languages, like Java are mostly straightforward for me.
Diapolo10 2 points · Dec 2022 · I write code for a living -- https://github.com/Diapolo10
I agree that seeing some highly unusual solutions to programming puzzles is highly amusing, but we should keep in mind that they're not practical.
Personally I've written a Python one-liner that solves Project Euler problem 22, and it's one of my favourite toy programs, but I'd never use those tricks in production code.
desrtfx [OP] 1 points · Dec 2022 · but other languages pro
As a professional programmer, I absolutely have to agree to everything you said here.
Code golf is one thing, production code is another.
In production code, we always have to strive for clean code principles.
Readability and maintainability always win over brevity.
Diapolo10 2 points · Dec 2022 · I write code for a living -- https://github.com/Diapolo10
I know. :D
If I saw this in a code review I'd probably start crying, but as code golf it was quite neat:
desrtfx [OP] 2 points · Dec 2022 · but other languages pro
Uh...Oh
Well done for code golf.
Took quite some time to digest this, but it is genius!