5 ms·
> `k = d.keys(); k.sort()`? Just give me a list Here you go, a sorted list of dict keys: k = sorted(d.keys())
by narimiran 9y ago
> `k = d.keys(); k.sort()`? Just give me a list
Here you go, a sorted list of dict keys:
k = sorted(d.keys())
- Mizza 9y agoYou'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.
- narimiran 9y ago> 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.
- masklinn 9y ago> 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.
- xapata 9y agoI 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."
- pythongen 9y agoyour 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.
- SmirkingRevenge 9y agoFYI... 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.
- int_19h 9y ago> 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.
- kortex 9y agoIn [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.