51 votes
Accepted

Is there a built-in equivalent to Python's enumerate?

Streaming` module - general, and the case at hand Starting with V10.1, there is an undocumented support for certain lazy operations in Mathematica. However, the ...
user avatar
46 votes
Accepted

Why do Mathematica list indices start at 1?

I think Leonid's answer deserves to be expanded upon. Most other languages are not symbolic, and thus the "variable name" is not something one needs to keep track of --- ultimately the interpreted or ...
user avatar
  • 5,454
45 votes

How to understand the usage of Inner and Outer figuratively?

Animated Mathematica Functions contains cool animated illustrations of the way a number of built-in functions work. Among them are Thread Inner: Outer See also: cormullion's video
user avatar
  • 350k
44 votes
Accepted

How to implement the general array broadcasting method from NumPy?

Broadcasting vs Listability NumPy broadcasting lets you perform, in efficient way, element-wise operations on arrays, as long as dimensions of those arrays are considered "compatible" in some sense. ...
user avatar
  • 14.7k
36 votes
Accepted

Apply multiple functions to same list

Also, y = {1, 2, 3, 4, 5, 6, 7}; #[y] & /@ {Max, Min, Median, Mean} (* {7, 1, 4, 4} *) EDIT: comparing the timings: ...
user avatar
  • 121k
35 votes

Selecting every n-th element from a list

Please look up Part and Span. You can use data[[ ;; ;; n]]
user avatar
  • 225k
34 votes
Accepted

DeleteDuplicatesBy is not performing as I'd hoped. Am I missing something?

The behavior described here is the same from 10.0.0 up to at least 10.3. Summary We can look at the code of DeleteDuplicatesBy and it turns out it uses ...
user avatar
33 votes

Apply multiple functions to same list

You can use Through. Through[{Max, Min, Median, Mean}[y]] (* {7, 1, 4, 4} *) Hope this helps.
user avatar
  • 39.4k
33 votes
Accepted

Why it seems that there is no counterpart function as to multiplication, to `Total` as to addition?

In addition to the previous answer... I designed and implemented Total 17 years ago. Its first version was named ListSum. The ...
user avatar
31 votes

How to subtract the column means from each row of a matrix?

It is there: Standardize[mat, Mean, 1 &]
user avatar
  • 133k
31 votes
Accepted

Can Mathematica put these puzzle pieces together?

Assuming polygons follow the same (clockwise or counterclockwise) vertex order, find all good quality two line segment rigid mappings between polygons without overlap with each other (at least much ...
user avatar
  • 14.7k
30 votes
Accepted

Is there a way to riffle more than two lists?

{{a, b, c}, {1, 2, 3}, {x, y, z}} ~Flatten~ {2, 1} {a, 1, x, b, 2, y, c, 3, z}
user avatar
  • 83.5k
30 votes
Accepted

Add a vector to a list of vectors

I recommend using Transpose twice since it is more efficient than other approaches. Moreover Plus has the ...
user avatar
  • 54.1k
30 votes
Accepted

Mysterious behaviour of Pick

This is not a bug. It is a consequence of the manner in which Pick scans its arguments. The Pick Process The documentation ...
user avatar
  • 67k
29 votes

What is a Mathematica packed array?

I would like to point out that Listable in a pure Function effectively unpacks the array, and makes it much slower than Map for ...
user avatar
29 votes
Accepted

How to understand the usage of Inner and Outer figuratively?

I think of Outer just like nikie showed. Inner is a generalization of matrix multiplication. I like the picture from the ...
user avatar
29 votes

How do you concatenate matrices? And how do you make block matrices?

As Daniel Lichtblau suggested in the comment, ArrayFlatten is the way. ArrayFlatten[{{A1, A2}}] // MatrixForm gives your ...
user avatar
  • 5,059
29 votes

How to implement the general array broadcasting method from NumPy?

Challenging NumPy's performance will be extremely difficult, and thus the effort of implementing this is not likely to be worthwhile. The reason is that the multiple-transpose method, even though it ...
user avatar
  • 22.5k
29 votes

Cyclic Take function?

A variation on Szabolcs's suggestion: overTake = PadRight[##, "Periodic"] &; overTake[{1, 2}, 5] {1, 2, 1, 2, 1}
user avatar
  • 350k
28 votes
Accepted

Performance of Select

res1 = Select[coordinates, #[[1]] > 6 && #[[1]] < 7 &]; // AbsoluteTiming // First 6.997629 ...
user avatar
  • 350k
27 votes
Accepted

Solving and Animating Three Body Problem

Generally always check Demonstrations site for good code. I cannot not mention an excellent "classic" of planar three body problem by Stephen Wolfram and Michael Trott. Code is short and I verified it ...
user avatar
26 votes
Accepted

Partition on sublists beginning with a certain marker elements

Split[list, (#2 =!= {a}) &] ...
user avatar
  • 133k
26 votes
Accepted

Partitioning a list when the cumulative sum exceeds 1

dat = {0.71, 0.685, 0.16, 0.82, 0.73, 0.44, 0.89, 0.02, 0.47, 0.65}; Module[{t = 0}, Split[dat, (t += #) <= 1 || (t = 0) &] ] ...
user avatar
  • 264k
26 votes
Accepted

How was Nothing implemented?

Ok, I failed to find a duplicate so here is my comment: I don't know how Nothing is internally implemented but you can do something like this with ...
user avatar
  • 133k
26 votes
Accepted

How to replace all elements of a list by a rule "element" -> "element_"

Here is one way: Pattern[#, Blank[]] & /@ {a, b, c, d, e, f, g, h, i, j} (* {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} *) An inspection of the ...
user avatar
  • 67k
25 votes

Permutations[Range[12]] produces an error instead of a list

Since Mathematica 8 it is possible generate the elements of any group one by one with GroupElements. Here's for example a randomly chosen element of the permutation ...
user avatar
  • 5,921
25 votes

Partitioning with varying partition size

New in 11.2 is TakeList: TakeList[Range[10], {2, 3, 5}] {{1, 2}, {3, 4, 5}, {6, 7, 8, 9, 10}}
user avatar
  • 124k
25 votes

Converting a list of rules to a list of lists

list = {"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98}; List @@@ list {{"Joe", 94}, {"Jane", 85}, {"Bob", 82}, {"Bill", ...
user avatar
  • 34.5k
25 votes

How to subtract the column means from each row of a matrix?

Well, transposing, subtracting, transposing... Transpose[Transpose[mat] - Mean[mat]]
user avatar
  • 111k
25 votes
Accepted

Solving word search puzzles

Here we go... ...
user avatar
  • 68.8k

Only top scored, non community-wiki answers of a minimum length are eligible