0

I have just come across an interesting case, I think.

I was trying to reverse my list and then print output using the print() function.

Here are the two ways I have tried:

1. Printing directly:

x = [2.0, 9.1,12.5]
print(x.reverse())

output: None

2. Using f string:

x = [2.0, 9.1,12.5]
print(f"The reverse of x is {x.reverse()}")

output: The reverse of x is None

The output from both methods is None as you can see above.

Can anyone explaing why both methods produce None?

P.S. I know that the this method works and prints revered list

x.reverse()
print(x)

but I am not intereseted in this? I want to find out why both methods above produce None.

G.T.
  • 130
  • 9

4 Answers4

2

x.reverse() is a so called in-place operation, that means the list stored in the variable x is modified (reversed) and the output of x.reverse() is None. Hence, when you run print(x.reverse()) it prints the output which in this case it None.

P.S.

If you need an operation which returns a reversed copy of the original list, you can use reversed() more info here:

x = [2.0, 9.1, 12.5]
print(list(reversed(x)))

(Note: list is used to convert the iterator result of reversed to a list).

The second option is to create a reversed copy via slicing (thanks @Andrey):

x = [2.0, 9.1, 12.5]
print(x[::-1])

More context:

In general, in-place operations return None, that helps prevent mistakes and distinguish in-place and normal operations.

G.T.
  • 130
  • 9
mikulatomas
  • 330
  • 1
  • 12
0

You can reverse and print it like this:

print(x[::-1])
Andrey
  • 400
  • 2
  • 8
0

Have you tried this:

x = [2.0, 9.1,12.5]
x.reverse()
print(x)

It should in theory work. Don't flame me if it doesn't I am quite new to stack overflow so I am not too sure on how to post answers and the sort.

0

You can reverse the list and print it by using the following code!

# create a list 
mylist = [2.0, 9.1,12.5] 
x.reverse() # Reverse the list
print(mylist) # Print the list


# Output: [12.5, 9.1, 2.0]
Omar The Dev
  • 123
  • 1
  • 14
  • This is definitely bad pattern! Atleast rename the function to `print_reversed_list()`. – mikulatomas Jan 29 '22 at 09:33
  • ***It's not definitely bad pattern*** I made the function with that name, but it is also understandable. – Omar The Dev Jan 29 '22 at 09:38
  • 1
    If side-effect of the function is outside the scope of the functions with fixed variable name `x` its definitely bad pattern. – mikulatomas Jan 29 '22 at 09:39
  • I made it x, so he can use the code instantly, even the asker can understand because he set the list to x – Omar The Dev Jan 29 '22 at 09:40
  • 1
    I understand, but teaching this way of thinking can be problematic in the long run (in other tasks, where side-effect can be hard to debug). – mikulatomas Jan 29 '22 at 09:43
  • @mikulatomas What is the problem with x can u explain? – G.T. Jan 29 '22 at 10:09
  • @G.T. `x` is variable outside so called *scope* of the function `print_reversed_list`. It CAN be dangerous to modify value/content of variable outside the function. The name is not the problem here (but also, I would prefer some more descriptive name). The good pattern would be to pass the list as *argument*, reverse it and return reversed list - then print it with `print()` function. – mikulatomas Jan 29 '22 at 10:28
  • @G.T. You can see my answer below for more details. – mikulatomas Jan 29 '22 at 10:31