Python Forum
Why is this function printing the number of keys in a dictionary? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Why is this function printing the number of keys in a dictionary? (/thread-21145.html)



Why is this function printing the number of keys in a dictionary? - mafr97 - Sep-16-2019

This is a HOMEWORK problem, that I am struggling with and want to understand.

We are asked to create a function that will take in a dictionary and return the list in ascending order, either alphabetically or numerically.

This is my code:
def dictKeysOnly (dictionary):
    lst = [dictionary]
    lst = sorted(lst)
    return (lst)
print("List in ascending order: ",sorted(dictionary))
Here is what I get when run the function. I know I don't have an input, but usually it just gives a blank box (I'm running this in Jupiter Lab just to test it). Then I'll run it with an input to test the outcome.
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-3c0c43693ca8> in <module> 3 lst = sorted(lst) 4 return (lst) ----> 5 print("List in ascending order: ",sorted(dictionary)) NameError: name 'dictionary' is not defined
I usually miss something when I make a function because it's not always super clear to me, but could someone explain how I define my dictionary input. Also why isn't me inputting 'dictionary' in the function defining it as a parameter?


RE: Why is this function printing the number of keys in a dictionary? - woooee - Sep-16-2019

Can't tell from your code because it isn't formatted, but it looks like dictionary is only declared within the function, so is not defined anywhere outside the function. i.e. for the print statement. Also, the function is never called and so will never execute, and there is no reason to sort the list as it has only one item, a dictionary.


RE: Why is this function printing the number of keys in a dictionary? - mafr97 - Sep-16-2019

(Sep-16-2019, 08:13 PM)woooee Wrote: Can't tell from your code because it isn't formatted, but it looks like dictionary is only declared within the function, so is not defined anywhere outside the function. i.e. for the print statement. Also, the function is never called and so will never execute, and there is no reason to sort the list as it has only one item, a dictionary.

My bad, didn't even notice. Here it is:

def dictKeysOnly (dictionary):
>>>lst = [{dictionary}]
>>>lst = sorted(lst)
>>>return (lst)
>>>print("List in ascending order: ",sorted(dictionary))

Edit: I'm not sure how to indent, but everything under the function is indented once.

When I try to call the function, here is what I get back.

dictKeysOnly({"kelly","amber","sophia","henry","zak"})

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-25723a87e72e> in <module>
----> 1 dictKeysOnly({"kelly","amber","sophia","henry","zak"})

<ipython-input-3-93a6185952c3> in dictKeysOnly(dictionary)
1 def dictKeysOnly (dictionary):
----> 2 lst = [{dictionary}]
3 lst = sorted(lst)
4 return (lst)
5 print("List in ascending order: ",sorted(dictionary))

TypeError: unhashable type: 'set'

What do you mean by it's not defined outside the function, and how would I fix that? And how would I give it a reason to sort the list.


RE: Why is this function printing the number of keys in a dictionary? - stullis - Sep-16-2019

For posting on the forum, use the
 tags to present your code with syntax highlighting. It makes things much easier.

As for issues with your code, line 5 is on the same level as the return in the function. As a result, the interpreter is evidently evaluating that line outside of the function scope.

In

dictKeysOnly({"kelly","amber","sophia","henry","zak"})
That is a set, not a dictionary. You can convert a set to a list with list().

def dictKeysOnly (dictionary):
    lst = list(dictionary)
    return sorted(lst)
For a dictionary, you can just use dict.keys() to get a list of the keys.


RE: Why is this function printing the number of keys in a dictionary? - woooee - Sep-17-2019

Quote:1 def dictKeysOnly (dictionary):
----> 2 lst = [{dictionary}]
3 lst = sorted(lst)
4 return (lst)
5 print("List in ascending order: ",sorted(dictionary))
This is different from the first code you posted, and as such shows a different error message. Note that the print statement will never be reached if is is indeed under the def.
Quote:What do you mean by it's not defined outside the function
The original message "NameError: name 'dictionary' is not defined" which was likely caused by the print statement not being indented under the function def.


RE: Why is this function printing the number of keys in a dictionary? - perfringo - Sep-17-2019

(Sep-16-2019, 07:20 PM)mafr97 Wrote: We are asked to create a function that will take in a dictionary and return the list in ascending order, either alphabetically or numerically.

What does mean 'return list in ascending order'? List of keys? List of values? List of key-value pairs?

One don't need complicated conversions, dictionary keys can be sorted just like sorted(d), if values needed to be sorted then sorted(d.values()) and to sort list of key-value tuples sorted(d.items()). Sorted function always returns list.