Skip to main content
Engineering LibreTexts

14.6: Iterating through the Keys and Values of a Series

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

    Finally, it’s common to need access to both halves of each key/value pair as you iterate through a Series. The way to accomplish this is to call the .items() method of the Series. But it’s tricky, because when you use .items() you assign two variables in your loop instead of just one.

    Before showing the complete loop, let’s focus on just the loop header needed for this technique:

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

    for secret_identity, hero in alter_egos.items():

    I named two loop variables, separated by a comma. The reason I put secret_identity first is that in this Series, we used Bruce, Peter, etc. as the keys, with the superhero names as the values. And with .items(), the variable name you want to use for the key is listed first.

    The rest of the loop follows logically from this, with both variables available inside the loop body:

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

    print("We're now going to recognize some outstanding citizens.") for secret_identity, hero in alter_egos.items():

    print("{}, known to his friends as {}.".format(hero, secret_identity))

    print("The crowd screams: 'YAY {}!'".format(hero.upper()))

    print("Thanks, everyone, for your service.")

    If we freeze the program just after the third execution of the loop header this time, we get the picture in Figure 14.6.1. And the output, of course, is:

    | We're now going to recognize some outstanding citizens.

    | Hulk, known to his friends as Bruce.

    | The crowd screams: 'YAY HULK!'

    | Spidey, known to his friends as Peter.

    | The crowd screams: 'YAY SPIDEY!'

    | Iron Man, known to his friends as Tony.

    | The crowd screams: 'YAY IRON MAN!'

    | Thor, known to his friends as Thor.

    | The crowd screams: 'YAY THOR!'

    | Thanks, everyone, for your service.

    clipboard_ebc3c7804e94bb2fcb36c41b5eae51e6b.png

    Figure \(\PageIndex{1}\): A snapshot of memory immediately after the third execution of the loop header in the alter_egos program.


    This page titled 14.6: Iterating through the Keys and Values of a Series 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.