DEV Community

Cover image for Dict Moves in Python

Dict Moves in Python

Ryan Palo on April 08, 2018

Quick tip time! Today, I started the #100DaysOfCode challenge again (for the millionth time). I'm determined to actually succeed at this challeng...
Collapse
 
erebos-manannan profile image
Erebos Manannán • Edited

You could also just use dict.get(letter, 0).

There's also no need to use "D G" and then .split(), you could just do:

SCRABBLE_SCORES = [
  (1, "EAOINRTLSU"),
  #... 
]

LETTER_SCORES = {
    letter: score for score, letters in scrabble_scores
    for letter in letters
}

#...

i.e.:

>>> for letter in "abc": print(letter)
...
a
b
c
Collapse
 
rpalo profile image
Ryan Palo

Hi! Thanks for the .get(letter, 0) tip. I've added it to the post, since I definitely agree that that's the best solution.

As far as the LETTER_SCORES variable goes, that was actually just provided as part of the setup of the problem, so I didn't bother to change that. You'll have to submit a pull request on the original repo :)

Thanks again for reaching out, though. I always love to learn that thing that makes my code that much cleaner!

Collapse
 
ahopkins profile image
Adam Hopkins

Nice writeup. I have been writing python for 15+ years, but I never ran int(). Thanks for sharing, and good article.