Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I utilized the above code to reverse a list yet I was asking for what reason is the range(len(my_list)) vital, all the more explicitly, for what reason doesn't it work in the event that I basically "for i in my_list:"?

my_list=[1,2,3,4,5]

new_list=[]

for i in range(len(my_list)):

    new_list.insert(i,my_list[-1])

    my_list.pop(-1)

1 Answer

0 votes
by (26.4k points)

Try the following code:

list1=[1,2,3,4,5]

for i in range( len(list1) - 1, -1, -1) :

    print(list1[i],end=' ')

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
4 answers
0 votes
1 answer
asked Jul 18, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 25, 2021 in Java by dante07 (13.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...