Python Forum

Full Version: What don't I understand about dict()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear friends,

There's something in either the dict() or the random.choice() that I don't understand fully I suppose. Can you shed some light on that for me.

import random
capitals_dict = {
'Alabama': 'Montgomery',
'Alaska': 'Juneau',
'Arizona': 'Phoenix',
'Arkansas': 'Little Rock',
'California': 'Sacramento',
'Colorado': 'Denver',
'Connecticut': 'Hartford',
'Delaware': 'Dover',
'Florida': 'Tallahassee',
'Georgia': 'Atlanta',
}
states = list((capitals_dict.keys()))
rand_choice = random.sample(states, 1)
print (rand_choice)
print(capitals_dict[rand_choice])
I don't understand why this is "unmashable" as it tells me. I've of course tried to "string" the return from the random.sample() but with no luck, so there's clearly something I simply don't understand. Can you assist?

Thanks
random.sample returns a list, you want random.choice which will return a single item.
THANKS... That's exactly the answer I was looking for.

Cheers