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
Keifer Joedicker
5,869 PointsHow to find how many values are in a key? I keep getting back each character counted as opposed to a whole value.
I'm trying to find how many values are assigned to a key than reassign the number to the key in a new dictionary. I keep getting back the values counted by character instead of counted in whole as one. What am I doing wrong?
Keifer Joedicker
5,869 PointsAlexander Davidson Say the value was "Hey" I get back 3 as apposed to counting the value as 1
2 Answers
Grant Tribble
8,210 PointsHi Keifer, Man! This was a good one!! So, if you are in control of how you load up your dictionary... Then do it like Ryan suggests and make the values of the keys in lists, tuples or even sets (even if it's just one item). If you're not in control of the input dictionary, you'll need to write in a conditional that will sort those suckers out. If there are multiple values for any given key, they will have to be set in there as a list, tuple, set... and maybe some other cool data type that I'm not thinking of. However, if someone on the input side gets tricky with you and inputs a single string, it will be counted as a string and thus give you the length of that string instead of telling you that there's only one item there. So... if you want to catch that sneaky little guy, you just need to set a trap for a datatype 'str'!!! Remember, if it's not a string, it will be in a datatype that can be counted the way you intended. There are probably much cleaner ways to do this, but i'm going to give you the down and dirty.
def num_courses(a):
dishes = {}
for i in a:
if type(a[i]) == type("any ole string"):
dishes[i] = 1
else:
dishes[i] = len(a[i])
return(dishes)
menu = {"Big Dinner" : ['Appetizer', 'Salad', 'Entre', 'Sides', 'Desert'], "Regular Dinner" : ('Salad', 'Entre', 'Sides') , "Little Dinner" : {'Entre', 'Sides'}, "Snack" : 'Appetizer'}
num_course(menu)
And that should do it!! Nobody will sneak a silly ole string in on you from there! Good luck!
Keifer Joedicker
5,869 PointsThank you that was really helpful!
Ryan S
27,276 PointsHi Keifer,
Do you mean that each key will potentially have more than one value and you want to count each value?
If I understand what you are asking then you will need to put each value in its own list, and find the length of that. If you only have a single string as a value then the len() function will count the number of characters. If you have a list of strings, then it will count each string as an item.
So your logic is fine for your function, but you will need to change the way you define the "hey" dictionary.
Try it like this:
hey = {'Name': ['Zara'], 'Age': ['7'], 'Class': ['First'],}
This way you can add however many values you want to each key and it will count them as you intend.
Hope this helps.
Keifer Joedicker
5,869 PointsThank you for your time it worked perfectly!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsWhat do you mean?