Insert element into numpy array and get all rolled permutations
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When working with NumPy, "insert an element and get all rolled permutations" usually means two separate operations. First, generate every array formed by inserting one value at each possible position. Then, for each resulting array, compute its cyclic rotations with np.roll. Keeping those steps separate makes the code clearer and avoids confusion about what counts as a unique result.
Insert the Element at Every Position
If the source array has length n, there are n + 1 insertion positions. A small helper function makes that explicit:
Output:
This gives you every insertion result, but not the rolled variants yet.
Generate All Cyclic Rolls
For one array, all cyclic rotations can be produced with np.roll:
Output:
That is a cyclic rotation set, not a full factorial permutation set. The distinction matters because the number of cyclic rolls is n, while the number of full permutations is n!.
Combine Insertion and Rolling
Once the two operations are defined, combining them is simple:
This returns every rolled version of every inserted variant. Depending on the data, some of those results may repeat.
Remove Duplicates When Values Repeat
If the source array already contains duplicate values, insertion plus rolling can generate identical arrays more than once. In that case, normalize arrays into tuples and use a set:
Using tuples for deduplication is usually the simplest approach because NumPy arrays themselves are not hashable.
Build a 2D Result Array When Shapes Match
If you want to keep the results in one NumPy structure, stack them into a 2D array:
This is handy for downstream vectorized processing, but it can consume a lot of memory if the input grows. Remember that the result count is (n + 1) * (n + 1) before deduplication.
Be Precise About the Goal
This topic often becomes messy because "rolled permutations" is ambiguous. You should decide which of these you actually need:
- every insertion position only
- every cyclic roll of one inserted array
- every cyclic roll of every insertion result
- all full permutations after insertion
Those are different problems with very different result sizes. If the requirement is truly "all full permutations," use itertools.permutations instead of np.roll.
Common Pitfalls
- Mixing up cyclic rolls with full permutations and underestimating the difference in result count.
- Calling
np.insertonce and expecting it to generate every insertion position automatically. - Ignoring duplicate results when the original array contains repeated values.
- Forcing everything into one large stacked array before checking memory cost.
- Writing the transformation as one dense expression instead of separating insertion and rolling into testable steps.
Summary
- Treat insertion and cyclic rolling as two separate operations.
- Use
np.insertacross every index from0ton. - Use
np.rollto generate cyclic rotations for each inserted result. - Deduplicate with tuples when repeated values can create identical arrays.
- Clarify whether you need rolls or full permutations before choosing the implementation.
Related reading
- Insert or delete a step in scikit-learn Pipeline
- Insert result of sklearn CountVectorizer in a pandas dataframe
- Inserting image into IPython notebook markdown
- Installation Issue with matplotlib Python
- Inserting an equal value element
- Insertion sort better than Bubble sort?
- Integer division algorithm
- Integer division How do you produce a double?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.