Addition of inplace argument to sorted

current scenario -

  1. there are two ways to sort, (let us say a list)
lst_1 = [1, 4, 3, 2, 5]
sorted(lst_1) # this is not in place
lst_2 = [3, 2, 1, 4, 5]
lst_2.sort() # this is in place
  1. for a new user, it is confusing to tell which one sorts the list in place, which is against,
    Explicit is better than implicit.
  2. every time one wants to sort, one has to make a choice, whether to use sorted or sort

expected scenario -

  1. add an inplace argument to sorted, which would work like,
lst_1 = [1, 4, 3, 2, 5]
sorted(lst_1, inplace=True)
lst_1

[1, 2, 3, 4, 5]
  1. this would reduce the need to use sort, and one would not have to make a choice every time one wants to sort

note -
the same pattern also applies for reverse vs reversed

[EDIT: ] My response below was based on misremembering the topic of that other…“topic”. We had in fact discussed reversed() which is a 'list_reverseiterator' object and has the present-tense/past-tense issue I describe here.


Since sorted() creates an iterator rather than a list, I think the main point of confusion about it is the function name. ~sorted()` sounds like “the list has been sorted

sorter(a_list) would be more true to form since it wraps a_list in an iterator. These two names are gramamatically congruent (sortER <-> iteratOR) as “a thing that does X”.

For anyone wishing a further read, docs.python.org/sorted(). We discussed sorter() versus sorted() at length recently, I’ll see if I can find it and post a link here.

[EDIT: ] The topic seems to have been deleted. What a shame… Still searching, though, in case I’m just not feeding the Search utility properly.

But sorted does create a list.

On Python 3.10:

print(type(sorted([3, 2, 1])))
# Output: <class 'list'>
1 Like

I class myself (relatively speaking) as a beginner and as such I soon learned that the myList.sort() method changes the order of elements in the original list, while the sorted(myList) function does not. That to me is the distinction – one is a ‘method’ the other is a ‘function’.

myList1 = [25, 50, 12, 4, 7]

myList2 = [45, 20, 4, 16, 32]

myList1.sort() # this has now altered (or 'sorted') myList1

myList3 = sorted(myList2) # I now have a new 'soreted list', but I can still work with the unsorted one

It’s possibly better to say that sorted()returns’ a sorted list, in the same way that the functions print() returns some output, and type() returns the object type.

edit: as politely pointed out to me, print() returns ‘None’ and is a bad example. My thanks @vbrozik

1 Like

“Since sorted() creates an iterator rather than a list”

No it doesn’t. It returns a list.

“for a new user, it is confusing to tell which one sorts the list in place”

It’s not confusing. They just have to learn what the function and method does. One sorts in place, the other returns a new list. Where is the confusion?

We should stop this harmful meme that anything that needs to be learned is “confusing”. People are capable of memorising tens of thousands of words, thousands of locations, hundreds of faces, dozens of grammatical and mathematical rules, and that’s before they even start learning to program.

"which is against, Explicit is better than implicit."

Both are equally explicit.

"every time one wants to sort, one has to make a choice, whether to use sorted or sort"

That choice is independent of how we spell the function. Whether it is spelled:

mylist.sort()
sorted(mylist)
mylist.sorted()
sorted(mylist, inplace=False)
sorted(mylist, inplace=True)
trier(mylist)
sortieren(mylist)
сортировать(mylist)
並べ替える(mylist)
modify(mylist, inplace=True, sort=True)

or any one of an infinite number of other possibilities, you still fundamentally have to make the choice “do I want to sort this list or make a sorted copy?”.

Adding extra spellings just make the choice harder. Now you will have people having to decide:

  1. Do I want to sort in place, or make a copy?
  2. If I want to sort in place, should I use the sort method or the sorted function? Which is better/faster/easier?

Two choices instead of one is not making things easier for beginners, it is making it harder.

And then you have to deal with a whole new class of errors:

values = (4, 1, 3, 5, 2, 0)
sorted(values, inplace=True)  # Fails.
1 Like

yes we are able to do this, but not always and not for everything.
let me give some examples,

  1. password - it is a one, most likely less than 15-character string, but people forget it, and it is like a worldwide problem, that is why we have options to save passwords. so, here we face difficulty in remembering one string.
  2. phone number - similar to password, even phone number is like a 10-15 character string, but we forget it, again that is why we have the option to save phone numbers on our phone.
  3. bank account number, passport number - again these are one strings, but it becomes difficult to remember them. you could argue that there are people who remember these, but in general it is difficult to remember them.

(point 4 is slightly off-topic, but someway related)

  1. our brain has limited memory, so, probably somebody who does not have a lot of information memorized would be at an advantage when it comes to beginning to learn to program, as compared to somebody who has a lot of information memorized. because the person with more unallocated memory, will be able to allocate it towards learning to program, whereas the person with less unallocated memory, would first have to free up some space, that is forget some things to make space for new things, and then allocate that freed up space towards programming. that is your whole logic behind children learning languages faster than adults.

this reply I am writing, involves 100s of words, but it appears to be less difficult than remembering a password. probably because password is like an isolated node (or it is not connected to any other information), whereas all the words I use in this paragraph are in some way connected to each other, so there is a stronger chance for the isolated node to be forgotten.

so, we could learn a lot of stuff, but it is not really that clear what all do we remember, and how long before we forget something, so for example, I could remember a phone number today, but if I never use that let us say for 3 months, and you ask me that phone number three months later, then there is a strong chance I would have forgotten it.

this sorted vs sort thing also appears to be a bit similar, like today I learn one of them is in place, other is not, I will be able to use it today, next day also, maybe next week also, but if I do not use it for let us say 4 months, and 4 months later you ask me, I might be confused again.

instead I would prefer to use only one of them, let us say I prefer sorted, I never use sort, for me sort is deprecated, and then the choice is, do I want to set inplace true or false.
the sorting a tuple with inplace true would have to raise an error, like immutable objects cannot be sorted inplace.

whereas, let us say for some other builtin like len, so, even if I dont use len for 3-4 months, and then after 3-4 months I suddenly use it. there is nothing confusing in it, there is nothing that I would forget about len.

on the other hand, if you made like list has a different way to get the length, maybe like length(list), tuple has a different way to get the length, maybe like, tuple.length(), then again there is a chance I would forget it in 3-4 months.

Your examples would be relevant if the function and method were spelled

mylist.M3ZjK0qiWFjBAcnwLJmg()

iyjlA7pWC1SEkngFEZ18Q(mylist)

but they aren’t. They are English words “sort” and “sorted”, not random passwords or arbitrary strings of digits.

You seem to think that it is a problem if you forget some details about programming. It isn’t. It is normal. There is a HUGE amount to learn about programming, many libraries that we only use once or twice and then don’t touch them for months or years. That’s why we have documentation, and the help() function, and google, and tab-completion, and Intellisense, and dir(). We have many ways to refresh our memories.

But I doubt you will forget the difference between sort and sorted. Your English is too good for that.

Cool. I didn’t do a type() command on it. We tried several other tests and proofs in the other (deleted) thread. It’s a total shame that it seems to be lost. I spent a while researching the difference between ~.reverse() and reversed() and tried to consult it before making my first reply here.

This is a persistent issue: Topic deleted by author

[EDIT: ] I pulled up the toy code I used for the deleted topic. We were discussing reversed(), which ia a 'list_reverseiterator' object, not sorted(). Apologies for the largely inapplicable info in my post above. I have corrected it as much as I can without losing context for the replies to it.

This is an important point. Languages, both spoken and programmed, are a system. What you’re learning is not rote memorization but *what the pieces of the system are and how they interact. In others words, how the system works. Slightly off topic, but not really: Japanese is taught as “that’s just the way it is” and “there are no hard rules; everything is contextual” and Japanese is famously difficult to learn. As an outsider, I see pattern so the system that natives don’t recognize–and they adamantly insist that these patterns don’t exist. This is unfortunate because Japanese is actually a very cool language once you drop the urge to make analogies with English.

On the other hand, @steven.daprano is completely right about eliminating the habit of declaring something “complicated” and “confusing” (neither of which is synonymous with ‘complex’) as a prelude to abandoning any further effort to understand it. If this habit attains critical mass and becomes pervasive it will kill our future as a society.

So any changes need to accommodate the system that’s present. Some changes will improve the system, some will degrade it so one must be wise about such changes.

do I want to set in place true or false

…or just use deepcopy to make the distinction?

for some other builtin like len , so, even if I dont use len for 3-4 months, and then after 3-4 months I suddenly use it. there is nothing confusing in it

…because, of course, len extends a standard English language element ‘length’. Importantly, it extends the language in a linear and unambiguous way without changing it. Using sorted() for something that hasn’t happened yet is misleading.

That is a very confusing statement. print() returns None If you mean the act of text printing it is a side-effect of the function not a return value. The distinction can be very important in some cases.

1 Like

I think it is slightly confusing, and harder to remember than necessary, but not for the reason the OP has laid out, and his proposal would make it worse.

It is just a matter of spelling. It is easy to remember that there are 2 different ways and how they differ. It is relatively hard (for me at least) to remember which is which, most of the time I have to look it up.

A better change, if anything were to change, would be just to rename (or alias) sorted() to sorted_copy() or similar.

Imagine you are giving a command to a list: “Sort yourself!”

So the list.sort() method is a command telling the list to sort itself.

That means that sorted() must be the function that returns a sorted list. Just as the name suggests :slight_smile:

Making an alias sorted_copy() will just mean that people will be confused by having three choices:

“What’s the difference between sort(), sorted() and sorted_copy()? Does that mean that sorted() doesn’t make a copy of the list?”

Then we have to explain that sorted_copy is just a copy of sorted.

(And before you ask, breaking backwards compatibility by removing sorted is not an option. We don’t lightly break people’s perfectly good working code if we can help it.)

Sorry, the existing names are just not suggestive to me the way they are to you. Maybe because I’m not a native English speaker, maybe because I’m “different”, maybe because at bottom I dislike OOP and prefer purely functional style (so I don’t think of a list as something I can “command”).

I understand the compatibility argument very well. All in all, not a big deal.

Then you can completely forget about list.sort() and write pure functional code :grin:

3 Likes