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
Max Lettenberger
2,017 PointsTuples: Struggling w/ dictionaries
I have no idea how to make a dictionary with more than key value pairs (vs. a key picking out more than one value). How do you [make?] one that can contain keys which pick out three other values, such as "PM" picking out "The Beatles", "Magical Mystery Tour" and "The Fool on the Hill"?
I don't even know where to begin.
Please help!
1 Answer
Michael Hulet
47,913 PointsOn its own, a single key must only reference a single value. In other words, you can't do exactly what you want to do, in this case. However a simple way around this restriction is to have the key correspond to an array, holding all the values you want. Using the examples in your question, a solution might look like this:
//You have a great taste in music, by the way
let dictionary = ["PM":["The Beatles", "Magical Mystery Tour", "The Fool on the Hill"]]
//You would reference the object you want like below. The next line will cause the constant "bandName" to equal "The Beatles"
let bandName = dictionary["PM"][0]
Max Lettenberger
2,017 PointsMax Lettenberger
2,017 PointsThank you very much, Michael! Great to see that you appreciate the music choice, too.