Opening the book…
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 was last modified on Sun, 18 Sep 2022 02:06:13 GMT and 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.
The book could not be loaded just now.