Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Dictionary comprehension

Just wondering, could it be possible to use comprehension in this example? If so, what would be the correct way to do it? Thanks for any info you can provide.

5 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

i was writing my answer when you posted the video link so i hadn't seen it. dict comps are used to create dicts, so i don't think they would be useful here since he's already got a dict.

Hmmm.... I thought I made that comment/question on the specific video. I wonder where it went .... hang on a sec, while I search for it.

ok, found it ... .here's that video, please take a look at 2:30:

https://teamtreehouse.com/library/using-databases-in-python/meet-peewee/queries-are-your-friend

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

dictionary comprehensions are similar to list and set comprehensions. inside curly braces {} you put k:v for .... k in some other collection or perhaps for k,v in another dictionary. so for example:

newDict = {k:v for k, v in oldDict.items() if v < 5}
newDict = {x:x**2 for x in range(8)}

so, I guess, the answer to my question is no ?