News for hackers. Stuff that matters.
Re:Key differences between Python 2.7.x and Python 3.x (2014) by narimiran on Sunday May 07, 2017 @03:39PM (#14287178)
> `k = d.keys(); k.sort()`? Just give me a list

Here you go, a sorted list of dict keys:

    k = sorted(d.keys())
Re:Key differences between Python 2.7.x and Python 3.x (2014) by Mizza on Sunday May 07, 2017 @03:48PM (#14287226)
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.

Re:Key differences between Python 2.7.x and Python 3.x (2014) by narimiran on Sunday May 07, 2017 @04:00PM (#14287290)
> 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.

Re:Key differences between Python 2.7.x and Python 3.x (2014) by masklinn on Sunday May 07, 2017 @05:33PM (#14287808)
> 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.

Re:Key differences between Python 2.7.x and Python 3.x (2014) by xapata on Monday May 08, 2017 @01:28AM (#14289441)
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."

Re:Key differences between Python 2.7.x and Python 3.x (2014) by narimiran on Monday May 08, 2017 @02:07AM (#14289580)
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) ;)
Re:Key differences between Python 2.7.x and Python 3.x (2014) by xapata on Monday May 08, 2017 @11:02AM (#14292080)
Sorry about that.
Re:Key differences between Python 2.7.x and Python 3.x (2014) by SmirkingRevenge on Sunday May 07, 2017 @04:10PM (#14287338)
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.
Re:Key differences between Python 2.7.x and Python 3.x (2014) by pythongen on Sunday May 07, 2017 @04:08PM (#14287327)
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.

Re:Key differences between Python 2.7.x and Python 3.x (2014) by int_19h on Sunday May 07, 2017 @11:46PM (#14289103)
> 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.

Re:Key differences between Python 2.7.x and Python 3.x (2014) by kortex on Monday May 08, 2017 @12:07AM (#14289177)
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.