Jul-02-2022, 03:41 PM
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’
reverse the elements of the ‘myList’ and store that in ‘reversedList’
how to reverse a list and store in another list in python
|
Jul-02-2022, 03:41 PM
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’
Jul-03-2022, 03:35 AM
(Jul-02-2022, 03:41 PM)SuperNinja3I3 Wrote: I want to reverse a list but want to store it in another list... 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.
Jul-04-2022, 04:42 AM
(This post was last modified: Jul-04-2022, 04:42 AM by deanhystad.)
reverse reverses the list in place and returns None.
x = list(range(10)) print(x.reverse()) 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
Jul-04-2022, 04:46 AM
(This post was last modified: Aug-14-2022, 10:08 AM by Yoriz.
Edit Reason: Formatting
)
(Jul-03-2022, 03:35 AM)XavierPlatinum Wrote:wow I never knew this is that much easy lol thanks btwreversedList = mylist.reverse()Now, if it is homework and you have to right a function to do this yourself, my hints would be:
Aug-14-2022, 09:59 AM
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)]
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=}')
Aug-14-2022, 06:36 PM
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:
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! |
|