Skip to main content
Engineering LibreTexts

14.5: Iterating Through the Keys of a Series

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

    I’m glad you asked. But in fact, you already know the answer: just use the .index syntax!

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

    print("Let's iterate through the keys instead:") for secret_identity in alter_egos.index:

    print("Nice to meet you, {}.".format(secret_identity))

    print("Carry on...")

    | Let's iterate through the keys instead:

    | Nice to meet you, Bruce.

    | Nice to meet you, Peter.

    | Nice to meet you, Tony.

    | Nice to meet you, Thor.

    | Carry on...

    By the way, you can see that the name of the loop variable is completely at your discretion. I called the previous one “hero” and this one “secret_identity” just because those names were reflective of their contents. But it’s really up to you: it has nothing to do with the name of the Series itself. (Yeah, I know the Marvel identities aren’t secret anymore, but I’m old school.)


    This page titled 14.5: Iterating Through the Keys 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.