-2

How to reverse list element one by one in python? I tried nested loop. But I couldn`t display second loop as "Result"

For example:

Input: 102 346 5897

Result: 201 643 7985

azro
  • 53,056
  • 7
  • 34
  • 70
Yahyobek
  • 17
  • 2

4 Answers4

0

Assuming your input list consists of strings, you go like this:

> a = ['102','346','5897']
> for i in a:
>    print(i[::-1],end=' ')

returns 201 643 7985

0

Try the following solution. you can use [::-1] to reverse a string.

x = ["123", "456", "789"]

y = []
for i in range(0,len(x)):
    y.append(x[i][::-1])
print(y)
['321', '654', '987']

You can use following trick with number inputs assuming inputs are numbers.

x = [123, 456, 789]
y = []
for i in range(0,len(x)):
    y.append(int(str(x[i])[::-1]))
print(y)
[321, 654, 987]
Jenny Patel
  • 39
  • 1
  • 6
-1

You can use [::-1]

string='102 346 5897'
lst=string.split()[::-1]
print(' '.join(lst))
-1

You can use the built in function 'reverse()' instead of for loop (more efficient code) :

code

def Reverse(lst):
   lst.reverse()
   return lst
  
lst = ["123", "456", "789"]
print(Reverse(lst))

output

['789', '456', '123']
s0n0fj0hn
  • 33
  • 5