Well, that had better be wrapped in a needless function, too:
# Correcterest way
assignSquaredListToVariable( dest_list, source_list);
or there'll be some serious code rewrites going on!Javascript might be easy to learn, but generally people don't do very complex things in Javascript. Actually, if you ignore the syntax, Javascript is closer in some ways to LISP than it is to C/C++/Java.
A function hides the implementation.
to traverse a list
Because there are different ways to traverse a list. An arraylist is easier to traverse with respect to CPU cycles and memory usage (typically) than a linked list. What does python use internally? I don't know, and I'd just as soon decide that myself...
square each element
While there are only so many ways to square numbers, for more complicated things, you are doing to want to change implementations while keeping it all hidden. In java, you are occasionally going to want to splice in native code.
recombine them into a list
I may not want to recombine them in a list, but square them in place. A function makes that trivial, and further makes it trivial to decide when and how to do that.
--> The real reason is one of readability. More english, less brackets and commas please.
Re: different ways to traverse a list and square elements, whenever the idiomatic implementations in your language are good enough for you, you just want to go with them. Hiding those details behind names is premature abstraction IMO. If you know you'll need your own list and square implementation, you can abstract that from the start. If you find needs for change that you didn't anticipate, the overhead of refactoring (or overloading operators, if it suits the problem) there and then is probably less than having abstracted everything that you might possibly want to change.
Deciding to square the list in place is not trivial at all. If you do that, you'd better change the signature of the function so that it doesn't return anything [Edit: or using whatever convention you have to highlight mutators, like bang! names in Scheme]. Otherwise, it's too easy to use the old list assuming it has the original values, or to mutate either the old or new references assuming they belong to independent lists.
Re: readability, you have to balance the familiarity of the syntax for a newcomer, and more importantly, the ease with which you can get acquainted to it, with its expressive power and its consistency with the rest of the language. If English was precise, concise, unambiguous, and consistent enough a notation, we wouldn't have many other programming languages.
More specifically, I find list comprehensions (and generator expressions in general) very quick to get used to, I find that they read well once you have acquainted yourself with them, I find that they have a great ink to information ratio that justifies making that initial learning investment, and I find that they feel right at home in Python.
Ah, here's a major difference between us: I'd rather not decide that myself, unless it becomes a problem.
dest = mapsquare(src)
Consider the possibility that C-like syntax and mixedCaseVerboseNames may be an acquired taste.
If you do square a lot other things than lists, it's probably best to say
dest = map(square, src)
For one-shot usage where defining square seems overkill, and where it's available and idiomatic, I find the list comprehension most readable.
Well, this is okay. Of course, in java, you have to hack it unpleasantly,
List dest = map(UtilityMethods.class, "square", src)
...you have to manually extract the method "square" from UtilityMethods.class and process it over src. One advantage: You can use UtilityMethods as a namespace and swap in different methods thereby.
...
# Pythonic way
...
# Correct way (...in any language, and if I see anything else in my co-workers code, you damn well better believe there will be a re-write):
List dest_list = squareEachElementInList(source_list);
The problem with languages like Lisp, Haskell, Erlang, etc, is that the syntax is completely atrocious. It's not just a matter of parenthesis or whatever. It's a matter of eye-straining horribleness. People have little trouble learning and using Javascript, for the simple reason that its syntax is reasonable.