Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> `k = d.keys(); k.sort()`? Just give me a list

Here you go, a sorted list of dict keys:

    k = sorted(d.keys())


You've completely missed the point. I'm not saying it can't be done, I'm saying that the new way is stupid.

In Python 3, I have to tell my students that

   breakfast = ["bacon", "eggs"]
   sorted_breakfast = breakfast.sort()
works, but

   breakfast = {"bacon": 2, "eggs": 3}
   sorted_breakfast = breakfast.keys().sort()
doesn't. They will ask, "why?", and I have to say "just because."

Instead, you have to use "sorted", even though "sorted" isn't even a verb, it is a property. "Why is sorted() rather than sort()" - "just because."

Similarly, "where is there a b'' in front of my name", why can't it find my module, and on and on and on.

The advantages of Python of a teaching languages are mostly gone now, and I would probably use JavaScript to teach beginners now, whereas I previously would have used Python2.7.


> In Python 3, I have to tell my students that

> breakfast = ["bacon", "eggs"]

> sorted_breakfast = breakfast.sort()

> works

Well, some of your students might point out to you that your `sorted_breakfast` is `None`, because that is what `sort()` returns, and even this example doesn't work as you have wanted ;)

On the other hand, if you have used `sorted(breakfast)` (and `sorted(breakfast.keys())` in other example), both examples would work as intended, and you couldn't blame Python 3.

> "Why is sorted() rather than sort()" - "just because."

It is not "just because", it is because it returns a sorted list (which is exactly what you wanted), compared to returning None.


> It is not "just because", it is because it returns a sorted list (which is exactly what you wanted), compared to returning None.

Also because `sorted()` works on any iterable, whereas .sort only works on lists.


I know that the best way to learn is "see one, do one, teach one" but I wish you'd put more effort into learning the language before teaching it.

The python.org tutorial and FAQ have answers to these questions.

I think it's unethical to tell a student, "just because" instead of, "I don't know why."


You're replying to the wrong person. I'm not the one who is doing teaching, I'm just the guy who corrects the teacher who hates Python 3 (for wrong reasons) ;)


Sorry about that.


FYI... neither of those work. Sort (using the dot sort() notation) operates on a mutable list in place, and returns no value. Never use it on the right side of an assignment. Its like dict's `update` in that regard.


your example sorted_breakfast doesn't work, in python 3.5 sorted_breakfast == None. Sort is a verb that modifies the receiving object, sorted is for returning a new object that is a sorted copy of the original, so you need to allocate two times the object.

Returning an iterator instead of a list has the advantage of not having to create a new object in case that is not needed, for example if you are going to filter a big dictionary just to obtain a few items then copying the keys of the dictionary and then filtering is not a good way of using memory efficiently.


> Similarly, "where is there a b'' in front of my name"

How do you even get them there? I mean, input() returns Unicode strings. So do all operations on text streams.


In [1]: breakfast = {"bacon": 2, "eggs": 3}

In [2]: sorted(breakfast)

Out[2]: ['bacon', 'eggs']

Done. One and only one obvious way to return a sorted list of keys of a dict.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: