How to loop over a list and print the index and the value of each item in the list using Python

2 Answers

0 votes
lst = ["python", "java", "c#", "c++", "php"]

for num, lang in enumerate(lst, start=1):
    print("Item {}: {}".format(num, lang))


'''
run:
 
Item 1: python
Item 2: java
Item 3: c#
Item 4: c++
Item 5: php

'''

 



answered Dec 7, 2017 by avibootz
0 votes
lst = ["python", "java", "c#", "c++", "php"]

for i in range(len(lst)):
    print("Item {}: {}".format(i + 1, lst[i]))


'''
run:
 
Item 1: python
Item 2: java
Item 3: c#
Item 4: c++
Item 5: php

'''

 



answered Dec 7, 2017 by avibootz

Related questions

...