Skip to content

Latest commit

 

History

History
20 lines (14 loc) · 473 Bytes

enumerate_in_loops.md

File metadata and controls

20 lines (14 loc) · 473 Bytes

Using enumerate for a counter in loops

names = ['paul', 'fiona', 'charlie']
for i, names in enumerate(names, start=1):
    print(f"{i}: {names}")

# output ---
1: paul
2: fiona
3: charlie

When you use enumerate(), the function gives you back two loop variables:

  • The count of the current iteration
  • The value of the item at the current iteration

More information on using enumerate() can be found here