What is the difference between `sorted(list)` vs `list.sort()`?

sorted() returns a new sorted list, leaving the original list unaffected. list.sort() sorts the list in-place, mutating the list indices, and returns None (like all in-place operations). sorted() works on any iterable, not just lists. Strings, tuples, dictionaries (you’ll get the keys), generators, etc., returning a list containing all elements, sorted. Use list.sort() when you … Read more

Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop

There are at least 6 (!) ways to clone an array: loop slice Array.from() concat spread operator (FASTEST) map A.map(function(e){return e;}); There has been a huuuge BENCHMARKS thread, providing following information: for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower. for other browsers while loop … Read more

How to deep copy a list?

E0_copy is not a deep copy. You don’t make a deep copy using list(). (Both list(…) and testList[:] are shallow copies.) You use copy.deepcopy(…) for deep copying a list. deepcopy(x, memo=None, _nil=[]) Deep copy operation on arbitrary Python objects. See the following snippet – >>> a = [[1, 2, 3], [4, 5, 6]] >>> b … Read more

Standard concise way to copy a file in Java?

I would avoid the use of a mega api like apache commons. This is a simplistic operation and its built into the JDK in the new NIO package. It was kind of already linked to in a previous answer, but the key method in the NIO api are the new functions “transferTo” and “transferFrom”. http://java.sun.com/javase/6/docs/api/java/nio/channels/FileChannel.html#transferTo(long,%20long,%20java.nio.channels.WritableByteChannel) … Read more

Make copy of an array

You can try using System.arraycopy() int[] src = new int[]{1,2,3,4,5}; int[] dest = new int[5]; System.arraycopy( src, 0, dest, 0, src.length ); But, probably better to use clone() in most cases: int[] src = … int[] dest = src.clone();

Understanding exactly when a data.table is a reference to (vs a copy of) another data.table

Yes, it’s subassignment in R using <- (or = or ->) that makes a copy of the whole object. You can trace that using tracemem(DT) and .Internal(inspect(DT)), as below. The data.table features := and set() assign by reference to whatever object they are passed. So if that object was previously copied (by a subassigning <- … Read more

What is the difference between shallow copy, deepcopy and normal assignment operation?

Normal assignment operations will simply point the new variable towards the existing object. The docs explain the difference between shallow and deep copies: The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object and … Read more