14.5: Iterating Through the Keys of a Series
- Page ID
- 39296
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.)