Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.