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.

iOS

jon kelson
jon kelson
5,149 Points

please can someone explain why we have the (key,value) and why in brackets ?.

when taught in the video we just put in a word by itself after for, so why the (key, value) after the for and what why use key after switch and value after append. could we use them the other way around ?

for (key, value) in world { switch key { case "BEL", "LIE", "BGR": europeanCapitals.append(value) case "IND", "VNM": asianCapitals.append(value) default: otherCapitals.append(value) } }

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Jon:

When using a for in loop while iterating through a dictionary, you should use a tuple.

Seems like world is a dictionary that holds capitals, and they want those to be appended to their corresponding continent.

for (key, value) in world {
 // Do work here
}

What is happening here is that the for in loop is iterating through the dictionary, and temporarily assigning the keys and values to these two constants key & value, where key holds the keys of the dictionary, and value holds the values of each key. Sounds a bit complicated, but it's not as bad.

// Dictionary
var cars = ["Key1":"BMW" , "Key2":"Ferrari"]

// This dictionary holds 2 items.

cars["Key1"] // Will return BMW

Now, in order for us to access these values, we can use a switch to switch on the keys, and get in return their value.

var cars = ["Key1":"BMW" , "Key2":"Ferrari"]

for (key, value) in cars {

    switch key {
    case "Key1" :
        print(value) // Will print BMW
    case "Key2":
        print(value) //Will print Ferrari
    default: break
    }  
}
}

To read more about tuples, you can go here and read on Basics. If you scroll down, you'll find tuples.

Hope this makes more sense to you now.

If any other questions, please let me know.

Good luck

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The key is the country abbreviation. The value is the name of the city. So we want our capital arrays to hold the names of the citys... not the countries. The name of the country doesn't tell us what the capital is. So for example, if our key is equal to "IND" the value "New Delhi" will be appended to the asianCapitals array. Hope this clears things up!

jon kelson
jon kelson
5,149 Points

Thank you so much for both of you for your help . Much appreciated !