Unable to reverse list in Python
Question:
I have tried several methods, and for some reason I cannot reverse this list. Here is my code:
array = []
elements = str(input().split())
array.append(elements)
array.reverse()
print(array)
This simply returns the original list. array[::-1]
does the same thing. The reversed
function outputs <list_reverseiterator object at 0x05AC1550>
. How can I do this?
Answers:
You are taking input then splitting it and then again converting it to string.
If you want to split your inputs into a List and then reverse it then you should try this:
array = []
elements = input().split()
print(elements)
elements.reverse()
print(elements)
See @Bishakh Ghosh answer first. As to why your code is not working:
elements
is an array (list). You are append
ing it to array
making it look like:
array = [[element1, element2, etc ]]
which is an array
(list) of an array
(list). You actually need to do this:
array += elements
Now array
will have this:
array = [element1, element2, etc]
This will work as you expect. The shorter (than your code) way will be:
print(input().split()[::-1])
In your code array
is instantiated as an empty list. Additionally, elements = str(input().split())
will create a list of items, however you need to specify the delimiter in your split
method ie split("")
or split(" ")
. As a result, you are appending a list of stuff (possibly an empty list) to an empty list. Like Bishakh said don’t convert an array into a string version of itself, which is why you are getting a memory location (since your compiler is treating the array as a string object and not an array).
Try something like
elements = input().split(" ")
elements.reverse() or elements = elements[::-1]
Assign the result:
array = array[::-1]
I have tried several methods, and for some reason I cannot reverse this list. Here is my code:
array = []
elements = str(input().split())
array.append(elements)
array.reverse()
print(array)
This simply returns the original list. array[::-1]
does the same thing. The reversed
function outputs <list_reverseiterator object at 0x05AC1550>
. How can I do this?
You are taking input then splitting it and then again converting it to string.
If you want to split your inputs into a List and then reverse it then you should try this:
array = []
elements = input().split()
print(elements)
elements.reverse()
print(elements)
See @Bishakh Ghosh answer first. As to why your code is not working:
elements
is an array (list). You are append
ing it to array
making it look like:
array = [[element1, element2, etc ]]
which is an array
(list) of an array
(list). You actually need to do this:
array += elements
Now array
will have this:
array = [element1, element2, etc]
This will work as you expect. The shorter (than your code) way will be:
print(input().split()[::-1])
In your code array
is instantiated as an empty list. Additionally, elements = str(input().split())
will create a list of items, however you need to specify the delimiter in your split
method ie split("")
or split(" ")
. As a result, you are appending a list of stuff (possibly an empty list) to an empty list. Like Bishakh said don’t convert an array into a string version of itself, which is why you are getting a memory location (since your compiler is treating the array as a string object and not an array).
Try something like
elements = input().split(" ")
elements.reverse() or elements = elements[::-1]
Assign the result:
array = array[::-1]