How to enumerate a list in Python

2 Answers

0 votes
lst = ["python", "java", "php", "c++"] 
 
list_tuple = list(enumerate(lst)) 
 
print(list_tuple[0])
print()
 
for val in list_tuple:
    print(val)
print()
 
for en, val in list_tuple:
    print(en, val)
  
 
 
 
'''
run:
   
(0, 'python')
 
(0, 'python')
(1, 'java')
(2, 'php')
(3, 'c++')
 
0 python
1 java
2 php
3 c++
  
'''

 



answered May 11, 2019 by avibootz
edited Apr 26, 2021 by avibootz
0 votes
lst = ["python", "java", "php", "c++"] 
  
list_tuple = list(enumerate(lst, 12)) 
  
print(list_tuple[0])
print()
  
for val in list_tuple:
    print(val)
print()
  
for en, val in list_tuple:
    print(en, val)
   
  
  
  
'''
run:
    
(12, 'python')
 
(12, 'python')
(13, 'java')
(14, 'php')
(15, 'c++')
 
12 python
13 java
14 php
15 c++
 
'''

 



answered Apr 26, 2021 by avibootz
edited Apr 26, 2021 by avibootz

Related questions

3 answers 212 views
1 answer 189 views
1 answer 175 views
1 answer 180 views
1 answer 148 views
...