Python Forum
What don't I understand about dict()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What don't I understand about dict()
#1
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
Reply
#2
random.sample returns a list, you want random.choice which will return a single item.
Reply
#3
THANKS... That's exactly the answer I was looking for.

Cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 72,484 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020