Ask coding questions

← Back to all posts
how do we number the list if we are doing it in a tupil
mahitaravi (0)

for eg:
wizard=['spells','books','hogwarts']
it would be printed as
1 spells
2 books
3 hogwarts

Comments
hotnewtop
mahitaravi (0)

Thanks for your comments

LAMAQDAHODWALA (61)

You can use the enumerate class. This returns a list of tuples with the index starting at 0.

for i,a in enumerate(wizard):
    print(f'{i+1}: a')
Wumi4 (531)

Use for i, x in arr.

Example:

nums = [1, 2, 3, 4, 5, 6]
for i, x in nums:
  print(f"{i} {x}")