How to traverse a list except the last element in Python

2 Answers

0 votes
lst = [34, 78, 90, 'python', 'java', 'c++']

for item in lst[:-1]:
    print(item)


     
 
'''
run:
 
34
78
90
python
java

'''

 



answered Jan 11, 2021 by avibootz
0 votes
lst = [34, 78, 90, 'python', 'java', 'c++']

index = 0
while index < len(lst) - 1:
    print(lst[index])
    index += 1


     
 
'''
run:
 
34
78
90
python
java

'''

 



answered Jan 11, 2021 by avibootz

Related questions

2 answers 189 views
1 answer 172 views
2 answers 120 views
...