Python Forum
how to reverse a list and store in another list in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to reverse a list and store in another list in python
#1
I want to reverse a list but want to store it in another list...

reverse the elements of the ‘myList’ and store that in ‘reversedList’
Reply
#2
(Jul-02-2022, 03:41 PM)SuperNinja3I3 Wrote: I want to reverse a list but want to store it in another list...

reverse the elements of the ‘myList’ and store that in ‘reversedList’

reversedList = mylist.reverse()
Now, if it is homework and you have to right a function to do this yourself, my hints would be:
#1  Using a negative indexes you can start at the end of the list and move backwards.
myList[-1] #the last item in the list
myList[-2] #the second to last item...  

# 2    Python's len() function tells you how many items in the list and therefor how times it needs to loop and copy the item to a new list.
Reply
#3
reverse reverses the list in place and returns None.
x = list(range(10))
print(x.reverse())
Output:
None
To use reverse() to make a reversed copy you first need to make a copy of the list, then reverse the copy.

Read about reverse() here:
https://docs.python.org/3/tutorial/datas...e-on-lists

Or you might use the built-in slice() function.

Read about slice() here:
https://docs.python.org/3/library/functi...lice#slice
Reply
#4
(Jul-03-2022, 03:35 AM)XavierPlatinum Wrote:
reversedList = mylist.reverse()
Now, if it is homework and you have to right a function to do this yourself, my hints would be:
#1  Using a negative indexes you can start at the end of the list and move backwards.
myList[-1] #the last item in the list

myList[-2] #the second to last item...  

# 2    Python's len() function tells you how many items in the list and therefor how times it needs to loop and copy the item to a new list.
wow I never knew this is that much easy lol thanks btw
Reply
#5
If you use .reverse() You don't see what's happening!

Better if you know what's going on, don't you think?

alphabet = 'abcdefghijklmnopqrstuvwxyz'

forward_list = [letter for letter in alphabet]

for i in range(len(alphabet)-1, -1, -1):
    print(alphabet[i])
    
backward_list = [alphabet[i] for i in range(len(alphabet)-1, -1, -1)]
# or
for i in range(-1, -(len(alphabet)+1), -1):
    print(alphabet[i])

backward_list = [alphabet[i] for i in range(-1, -(len(alphabet)  + 1), -1)]
BashBedlam likes this post
Reply
#6
This could be done by simply slicing the list.
items = [1, 5, 23, 67]
reversed_items_copy = items[::-1]
print(f'{items=}')
print(f'{reversed_items_copy=}')
Output:
items=[1, 5, 23, 67] reversed_items_copy=[67, 23, 5, 1]
rob101 likes this post
Reply
#7
The reverse() method on a list, does return None. It's an inline modification of the list.

my_list = [1, 2, 3]
my_list = my_list.reverse()
The last line does 3 things:
  • my_list is reversed inline
  • the reverse() method of my_list return None
  • None is assigned to the name my_list
  • The list, where the name my_list pointed before, disappears after the garbage collection

The problem here is a wrong assumption. list.sort and list.reverse are inline operations, which modifies the list itself and those operations return None.

If you want to sort or reverse an iterable, use sorted or reversed.
The sorted built-in function always returns a list.
The reversed function returns a reversed_iterator, which must be consumed by a list for example.

my_original_list = [1, 2, 3]

# all following lists do not modify my_original_list
# they are new lists
my_reversed_list_1 = list(reversed(my_original_list))
my_reversed_list_2 = my_original_list[::-1]


# sorting reverse
random_values = [44, 1, -10, 42, 1337, -5, -1, 10, 13, -27]

from_big_to_small = sorted(random_values, reverse=True)

# printing the id for each list object
# they are all different list objects in memory
print(id(my_original_list), id(my_reversed_list_1), id(my_reversed_list_2), sep="\n")
https://docs.python.org/3/library/functions.html#sorted
https://docs.python.org/3/library/functi...l#reversed
https://docs.python.org/3/howto/sorting.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python - List - Int List sophi 8 2,463 Apr-21-2022, 07:55 PM
Last Post: sophi
  Reverse Function in Python Voraman 7 3,190 Feb-13-2021, 07:21 PM
Last Post: Voraman
  reverse math in python, who will find the correct answer? Kakha 11 4,304 Jan-11-2021, 11:59 AM
Last Post: Gribouillis
  question on list in Python spalisetty06 1 2,019 Jun-30-2020, 09:03 AM
Last Post: pyzyx3qwerty
  Python list exercize ESTOUR 3 1,877 May-23-2020, 11:10 PM
Last Post: ESTOUR
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,882 May-15-2020, 04:57 PM
Last Post: snippsat
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,035 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  Check if a list exists in given list of lists Daniel94 2 2,207 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  list of strings to list of float undoredo 3 2,630 Feb-19-2020, 08:51 AM
Last Post: undoredo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,075 Oct-20-2019, 06:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020