Skip to main content
Engineering LibreTexts

9.6: More Exotic Array Modifications

  • Page ID
    39395
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    There are lots of additional things you can do to an array to either modify its structure or rearrange its contents. Here’s a few. Important: all of the functions in this section return a modified copy of the array you pass to it. They do not change the array in place.

    • np.append() can be used to add a single element to the end of an array, or to add an entire second array of elements to it. (In the latter case, this is really concatenation of arrays.)
    • np.insert() is like the first form of np.append(), except it inserts in the middle (or the beginning).
    • np.delete() will remove an element of an array by position. In other words, you tell it which index number to remove, not which element.
    • np.flip() reverses the order of elements in an array.

    These functions are all summarized in Figure 9.8.1.

    Remember that when you’re calling a function like this – which returns a modified copy – it is perfectly acceptable to store the return value in the same variable that you passed it. This is common if you don’t actually want to keep around the original:

    Code \(\PageIndex{1}\) (Python):

    ice_cream_flavors = np.flip(ice_cream_flavors)

    In this pattern, the net effect is effectively to modify the array in place, since you’re making a reversed copy, and then assigning that reversed copy to the same variable.

    Anyway, here’s some example code to illustrate the functions in this section:

    Code \(\PageIndex{2}\) (Python):

    clowns = np.array(["Bozo", "Krusty"])

    more_clowns = np.array(["Pennywise", "Skelton"])

    more_clowns = np.insert(more_clowns, 1, "Happy Slappy")

    all_clowns = np.append(clowns, more_clowns)

    all_clowns = np.append(all_clowns, "Ronald McDonald")

    all_clowns = np.flip(all_clowns)

    all_clowns = np.delete(all_clowns, 2)

    print("clowns is: {}".format(clowns))

    print("more_clowns is: {}".format(more_clowns))

    print("all_clowns is: {}".format(all_clowns))

    | clowns is: ['Bozo' 'Krusty']

    | more_clowns is: ['Pennywise' 'Happy Slappy' 'Skelton']

    | all_clowns is: ['Ronald' 'Skelton' 'Pennywise' 'Krusty' 'Bozo']

    An excellent exercise to help cement your understanding of the ideas in this chapter would be to go through the above “clowns” code line by line, drawing the memory picture as you go, and then confirm that your output matches the actual output.

    Yes, an excellent exercise indeed!


    This page titled 9.6: More Exotic Array Modifications is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.