Skip to main content
Engineering LibreTexts

9.2: Accessing Individual Elements

  • Page ID
    39391
  • \( \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}}\)

    Retrieving an Element

    To get the value of a specific element from an array, we use “boxie notation” with the index number:

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

    print(prez_elections[0])

    third_year = usa_years[2]

    print("{} was the 3rd year of U.S.A.".format(third_year))

    print("The highest-numbered player is {}".format( roster[len(roster)-1]))

    | 788.0

    | 1778 was the 3rd year of U.S.A.

    | The highest-numbered player is Christen Press.

    Remember, indices start at zero (not one) so that’s why the first line has a 0 in it.

    Now examine that last line, which is kind of tricky. Whenever you have boxies, you have to first evaluate the code inside them to get a number. That number is then the index number Python will look up in the array. In the last line above, the code inside the boxies is:

    ...len(roster)-1...

    Breaking it down, we know that len(roster) is 24, which means len(roster)-1 must be 23, and so roster[len(roster)-1] is Christen Press. It’s a common pattern to get the last element of an array.1

    To test your understanding, figure out what the following code will print:

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

    q = 2

    r = np.array([45,17,39,99])

    s = 1

    print(r[q-s+1]+3)

    The answer is at the end of the chapter.

    Changing an Element

    To modify an element of an array, just use the equals sign like we do for variables:

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

    stooges = np.array(['Larry','Beavis','Moe'])

    print(stooges)

    stooges[1] = 'Curly'

    print(stooges)

    | ['Larry' 'Beavis' 'Moe']

    | ['Larry' 'Curly' 'Moe']

    After all, an individual element like stooges[1] is itself a variable pretty much like any other.

    Slices

    Sometimes, instead of getting just one element from an array, we want to get a whole chunk of them at a time. We’re interested in the first ten elements, say, or the last twenty, or the fourteen elements starting at index 100. To do this sort of thing, we use a slice.

    Suppose we had a list of states in alphabetical order, and wanted to snag a chunk of consecutive entries out of the middle – say, Arizona through Colorado. Consider this code:

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

    states = np.array(["AL","AK","AZ","AR","CA","CO","CT", "DE","FL","GA","HI"])

    print(states[2:6])

    | ['AZ' 'AR' 'CA' 'CO']

    The “2:6” in the boxies tells Python that we want a slice with elements 2 through 5 (not through 6, as you’d expect). This is the same behavior we saw for np.arange() where the range goes up to, but not including, the last value. Just get used to it.

    We can also omit the number before the colon, which tells Python to start at the beginning of the array:

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

    print(states[:5])

    | ['AL' 'AK' 'AZ' 'AR' 'CA']

    or omit the number after the colon, which says to go until the end:

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

    print(states[8:])

    | ['FL' 'GA' 'HI']

    We can even include a second colon, after which a third number specifies a step size, or stride length. Consider:

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

    print(states[2:9:3])

    | ['AZ' 'CO' 'FL']

    This tells Python: “start the slice at element #2, and go up to (but not including) element #9, by threes.” If you count out the states by hand, you’ll see that Arizona is at index 2, Colorado is at index 5, and Florida is at index 8. Hence these are the three elements included in the slice.

    This slice stuff may seem esoteric, but it comes up surprisingly often.


    This page titled 9.2: Accessing Individual Elements 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.