0

I am writing a simple script, where I have a list of character and I want to loop over this list and reverse the ordered of 3 elements at a time. Ex. List: a,b,c,d,e,f ; Reversed list: c,b,a,f,e,d.

I tried to use print(list[0:2].reverse()) however when I tried printing this it outputted NONE My question is why does this happen?

FYI: I know there are other ways of doing this, I am just curious as to why this did not work.

Alen Paul Varghese
  • 1,278
  • 14
  • 27
mshark
  • 3
  • 3
  • Welcome to Stack Overflow. ```reverse()``` doesn't return a new list. It basically changes the original list. – ewokx Jun 15 '23 at 09:09
  • `reverse` is side-effecting - that is, it modifies the list in place, rather than returning a new one. – ndc85430 Jun 15 '23 at 09:09
  • sorry I wasnt very clear in the post. the line i used was `print(list[0:2].reverse())`, this prints NONE. I know that .reverse() does not create a new list. I am just wondering why it is not reverseing the order in the print – mshark Jun 15 '23 at 09:13
  • Does this answer your question? [How do I reverse a sublist in a list in place?](https://stackoverflow.com/questions/22257249/how-do-i-reverse-a-sublist-in-a-list-in-place) – Adam.Er8 Jun 15 '23 at 09:18
  • unfortunately no. I am trying to understand why this does not work, not how do I do the reversal. I already achieved this. My question is more of a theoretical question rather than a practical one. – mshark Jun 15 '23 at 09:25

2 Answers2

3
lst[0:2].reverse()

does not do much to the original list lst, because the slice operation lst[0:2] returns a new independent list object which is then reversed in-place by reverse. The original lst is unimpressed by this. You have to feed the reversed slice back with slice assignment to mutate the original. E.g.:

lst[0:2] = lst[0:2][::-1]

You could also use list.reverse's brother reversed , which does return an object (an reverse order iterator):

lst[0:2] = reversed(lst[0:2])
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • I understand that it does not affect the original list, my question is when i use print(list[0:2].reverse()) the output is NONE, so this means that .reverse does not work on subsets of the list. My question is why this is the case? – mshark Jun 15 '23 at 09:18
  • 3
    it prints `None`because `list.reverse()` returns `None` (since the list is modified in-place). if you want to print the list, you'll either need to use `reversed`(which does return something, as stated in the answer, i.e. `print(*reversed(lst))`), or do the reversing and printing that in separate statements (i.e. `lst.reverse(); print(lst)`). – Hoodlum Jun 15 '23 at 09:28
1

When you do a slicing operation e.g list[0:2], you essentially create a new list

Since reverse operates on the object you give it, but doesn't return anything, if you were to do print(list.reverse()) it would still print None because reverse doesn't return anything, but modify the object you use it on.

Now, since slicing creates a new list, when you do list[0:2].reverse() , you are modifying the list created by the slicing, which is also immediately lost since you don't assign it to any variable (and you can't since reverse doesn't return a value). Essentially, you are creating a list with the slicing, modying it, and then losing it forever

Additionally, you can assign the slicing to a variable and then use reverse on it, but it wouldn't be useful in your case

listed= ["a", "b", "c", "d", "e", "f"]
sliced = listed[0:2]
sliced.reverse()
print(sliced) prints ['b', 'a']

Hope this is clear

(Last advice, don't call your list "list", it shadows the built-in name)

  • I tried assigning it to a variable and printing that variable it still printed NONE. `ak=a[0:2].reverse()` `print(ak)` the ourput is still NONE PS: yes I am aware of not naming the list as 'list', in practice i dont do this, however for the sake of this question i just named it list here. – mshark Jun 15 '23 at 09:33
  • You did exactly what I warned in my post, assign the slice to a variable before, and then use reverse on that variable (no assignation), reverse modify the CURRENT OBJECT you are using it on, as explained on my answer, but doesn't return anything, var = smt.reverse() will always be None, but smt will be modified – Sparkling Marcel Jun 15 '23 at 09:36
  • Ohh my bad, this makes sense now. So even if I assign it like this var=listy[0:2].reverse() it will still be NONE as the return of the reverse is NONE. Thank you so much I finally get it now! – mshark Jun 15 '23 at 09:38
  • yes exactly! and since listy[0:2] creates a new list but you're never stocking it, using reverse on it is useless. Reverse modifies listy[0:2] which is a new list, but you never stocked this list beforhand, so it's lost, ence why you need to first store listy[0:2] in a var ( var = listy[0:2] ) and then use reverse on var ( var.reverse()) to modify var (note that I don't do var = var.reverse() because it would make var = None) – Sparkling Marcel Jun 15 '23 at 09:44