Here is the code itself:

j = len(list1) for j in range(len(list1),0): slovo += list1[j] j -= 1 

Python does not issue errors, it just stops at the moment between

 j = len(list1) и for j in range(len(list1,0): 
  • It’s not very good to give the same names to variables in the same context :) First, you have the variable j kept the length of 'list1', when the loop is executed, its value changes according to what the range returns and there’s no point in trying to change it - it will be for will take the next value from range - gil9red

2 answers 2

In general, the list is expanded:

 print (list1[::-1]) 
  • Thank! But now I just need to turn the list in a loop. Do you have any idea why the code doesn't work? - Michael
  • Because you have j - = 1 and you are not using a debugger. - Vladimir Martyanov
  • what is a debugger? - Michael
  • @Michael debugger is such a thing that allows you to see how the program actually works. Without mastering it in programming, there is nothing to consider. - Vladimir Martyanov
  • @ Vladimir Martianov, well, instead of the debugger, you can shove more print 's code and look at them as the algorithm works :) - gil9red

Do a coup through the for loop:

 items = [1, 2, 3, 4, 5, 6] new_items = [] for i in range(len(items)): new_items.append(items[len(items) - i - 1]) # OR: # for i in range(len(items), 0, -1): # new_items.append(items[i - 1]) # OR: # for i in reversed(range(len(items))): # new_items.append(items[i]) print(new_items) # [6, 5, 4, 3, 2, 1] 

Or done through while :

 items = [1, 2, 3, 4, 5, 6] new_items = [] index = len(items) - 1 while index >= 0: item = items[index] new_items.append(item) index -= 1 print(new_items) # [6, 5, 4, 3, 2, 1] 

In addition to the algorithm through the cycle and reversal through the slice ( [::-1] ), there are specific functions:

 list1.reverse() list1 = reversed(list1) # Чтобы получить именно тип список list1 = list(reversed(list1)) 

reverse expands the items in the list itself

reversed returns a list iterator with expanded items