0 votes
1 view
in Python by (11.8k 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 (24.6k 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
1 answer
asked Jul 18, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
0 votes
1 answer
asked Jan 8 in SQL by Appu (6.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...