reversed_strings = [x[::-1] for x in myList][::-1]
Answer from sjw on Stack OverflowPython - Function to reverse strings in LIST - Stack Overflow
python - How could could I reverse each string in the list? - Stack Overflow
python - list.reverse() -How to print reversed list of a string? - Stack Overflow
how to reverse string using lists python - Stack Overflow
Videos
reversed_strings = [x[::-1] for x in myList][::-1]
There are two things you need to use, the reverse function for lists, and the [::-1] to reverse strings. This can be done as a list comprehension as follows.
myList.reverse
newList = [x[::-1] for x in myList]
For your first function, that already exists as a built-in function of same name (sum()). For the second, you can use a simple list comprehension.
def rever(strings):
return [x[::-1] for x in strings]
Judging by your question it seems you are learning functions in python, you can reverse the list using a function like this:
strings_ = [str(x) for x in input("Please input at least 5 words (Use Space): ").split()]
for index,item in enumerate(strings_):
def recursion_reverse(string_1):
if not string_1:
return ""
else:
front_part=recursion_reverse(string_1[1:])
back_part=string_1[0]
return front_part+back_part[0]
strings_[index]=recursion_reverse(item)
print(strings_)
output:
Please input at least 5 words (Use Space): Hello world this is me
['olleH', 'dlrow', 'siht', 'si', 'em']
Surely by knowing what it does, you can figure the code. In the while loop, the index value starts from the end of the string and counts down to 0. In each step, it adds that character (again, starting from the end) to the end of the list it is building. Finally, it combines the list into a string.
So, given 'abcd', the list gets built:
'abcd' #3 -> ['d']
'abcd' #2 -> ['d','c']
'abcd' #1 -> ['d','c','b']
'abcd' #0 -> ['d','c','b','a']
Well basically, the get the length of the string with the len method. Which will return you an integer value representing how long that string is.
They then use the length of this string and effectively iterate down to zero in a while loop. Using the -= operator.
Each iteration (meaning each time around the loop) it will take away from length until it reaches zero.
So lets use hello as an example input and go through this together.
reverse_string('hello') is how we would call the method, done in the print statement of your code.
We then enter the function and perform these steps:
- We create a new empty array called
new_strings. - We find the length of the initial string
hellowhich returns us 5. Meaning that nowindexis equal to 5. - We create a while loop that keeps on going until
indexis no more usingwhile(index):- a while loop like this treats a0value asfalsyand will terminate upon reaching this. Therefore whenindexis0the loop will stop. - The first line of this loop performs
index -= 1which is the same as writingindex = index - 1so the very first loop through we getindex = 5 - 1and then nowindexis equal to4. - Because Python then lets us access the
characterof a string usingstring[index](and because this works from 0 -> n) performinghello[4]will in fact give us the charactero. - We also append this character to the array
new_stringsmeaning that as we go through the iterations to reach zero it will add each character backwards to this array giving us['o', 'l', 'l', 'e', 'h'] - Since index is now zero we leave the loop and perform a
joinoperation on the array to yet again create a string. The command''.join(new_strings)means that we wish to join the array we previously had without a separator. If we had done'#'.join(new_strings)we instead would have gotteno#l#l#e#hinstead ofolleh.
I hope this answer gives you some clarity.
How about this
x = input('enter the values:')
x = list(x)
res = []
for i in range(len(x) -1, -1, -1):
res.append(x[i])
print(res)
In addition to using reversed() or L[::-1], you could use list.reverse() to reverse the elements of the list in-place:
>>> L = ['1', '2', '3', '4', '5']
>>> L.reverse()
>>> L
['5', '4', '3', '2', '1']
You can implement reversed() yourself with a for loop:
>>> L = ['1', '2', '3', '4', '5']
>>> R = []
>>> for i in range(len(L)-1, -1, -1):
... R.append(L[i])
...
>>> R
['5', '4', '3', '2', '1']