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 Python Collections (Retired) Dictionaries Unpacking Dictionaries

Zachary Kaufman
Zachary Kaufman
1,463 Points

Dict Syntax

I was just wondering if you HAVE to write dictionaries as {"name": "Zach", "age": 15} or if you can do something like

my_dict = {

"name": "Zach",

"age": 15

}

does Python allow me to write dictionaries like that or do I have to do it all on one line? Thanks!

Zachary Kaufman
Zachary Kaufman
1,463 Points

Sorry about the weird formatting, when I first wrote this it out the second example all on one line again...

1 Answer

So yes, you can do

my_dict = {
  "username": "xela888",
  "language": "Python"
  "state": "CA"
}

I like that, it's javascripty. I wondered the same thing but figured it wouldn't work cause Python is all about indenting (which I really like but takes some getting used to) I suppose I could have just tried it but...I didn't.