from machine import Pin
import time
"""
# What enumerate() does
# enumerate() lets you loop over something and keep track of the position at the same time.
# Instead of this:
time.sleep(.2)
liste=["a", "b", "c"]
for item in liste:
print(item) # a b c vertical
print(liste) # ['a', 'b', 'c']
time.sleep(1)
i = 0
for item in ["a", "b", "c"]:
print(i,item) # 0 a,1 b,2 c vertical
i += 1
for item in ["a", "b", "c"]:
print(item) # a b c vertical
#************* enumerate
# Basic syntax
# enumerate(iterable, start=0)
# iterable → list, tuple, string, etc.
# start → number to start counting from (default is 0)
for i, item in enumerate(["a", "b", "c"]):
print(i, item) # 0 a,1 b,2 c vertical
# Starting from a different number
for i, item in enumerate(["x", "y", "z"], start=2):
print(i, item) # 3 a,4 b,5 c vertical
time.sleep(1)
myList=['x','y','z']
for i, item in enumerate(myList):
print(i, item) # 0 x,1y,2 z vertical
#********************************************
# Common real-world examples
#************ With a list
names = ["Alice", "Bob", "Charlie"]
for i, name in enumerate(names):
print(f"{i}: {name}") # 0: Alice 1: Bob 2: Charlie vertical
#************ With a string
for i, ch in enumerate("hello"):
print(i, ch)
# 0 h
# 1 e
# 2 l
# 3 l
# 4 o
#*****When you need the index to modify a list
nums = [10, 20, 30]
for i, val in enumerate(nums):
nums[i] = val * 2
print(nums) # [20, 40, 60]
"""
#*******************Dictionaries
# Nice one — enumerate() + dictionaries is a very common point of confusion,
# so let’s clear it up cleanly 😄
# Important thing to know first
# When you loop over a dictionary by default,
# you loop over its keys.
d = {"a": 10, "b": 20, "c": 30}
for x in d:
print(x)
time.sleep(1) # a,b,c
# 1️⃣ enumerate() over dictionary keys (most basic)
d = {"a": 10, "b": 20, "c": 30}
for i, key in enumerate(d):
print(i, key) # 0 c,1 a,2 b vertical
# 2️⃣ enumerate() with dict.keys() (same result, more explicit)
for i, key in enumerate(d.keys()):
print(i, key, d[key]) # 0 a 10,1 b 20,2 c 30 vertical
# 3️⃣ enumerate() with dict.values()
for i, value in enumerate(d.values()):
print(i, value) # 0 10,1 20,2 30 vertical
# 4️⃣ enumerate() with dict.items() (most useful)
# This is the one you’ll use all the time.
for i, (key, value) in enumerate(d.items()):
print(i, key, value) # 0 a 10,1 b 20,2 c 30 vertical
# 5️⃣ Starting index from 1 (common in reports / logs)
for i, (key, value) in enumerate(d.items(), start=1):
print(f"{i}. {key} = {value}")
# 1. a = 10
# 2. b = 20
# 3. c = 30