Help reversing a int list in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help reversing a int list in python

X = input() Y = X.split() for i in range(len(y)): k = [::-1] k[i]= int(k[i]) print(k) Prints the list converts it to int but doesn’t reverse it help

5th Jun 2022, 12:27 AM
X33X
X33X - avatar
17 Answers
+ 5
Whenever the user inputs the number via input() it will be a ***string***, not an integer in a list. Observe the difference, when you put: x = '12345' # input() y = [int(i) for i in x] print(y[::-1]) If you need help, you need to give the complete task description (+ course name).
5th Jun 2022, 7:20 AM
Lisa
Lisa - avatar
+ 3
3 options of many here x = input().split() # makes your list y = [int(i) for i in x] print(y[::-1]) #or b = [] for i in x: b.append(int(i)) print(b[::-1]) #or c = [] for i in range(len(x),0,-1): c.append(i) print(c)
5th Jun 2022, 12:50 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
X33X Try separating your numbers with a space, else the code reads it as a single number
5th Jun 2022, 3:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Ipang Good suggestion, else we gonna capsize soon. 🤣😂
5th Jun 2022, 3:45 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
I sure hope not Rik 😁
5th Jun 2022, 3:46 AM
Ipang
+ 1
For this <X> input "12 23 34 45 56" Is the result supposed to be like this? [ 56, 45, 34, 23, 12 ] Or this? [ 21, 32, 43, 54, 65 ] Or this? [ 65, 54, 43, 32, 21 ] Or something else?
5th Jun 2022, 2:02 AM
Ipang
+ 1
X33X How are you inputting your numbers. if you put them in as: 1 2 3 4 5, then the codes will work. if you input as 1,2,3,4,5, then you will need x = input.split(',') Let me know how you go, if you still can't get it, then I will write an example for you
5th Jun 2022, 3:40 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
X33X, Better post a link to the code in progress, so we can all stay in the same boat.
5th Jun 2022, 3:42 AM
Ipang
+ 1
Thanks that totally fixed it lol guess we didnt need to convert the str to a list first https://code.sololearn.com/cQ8nA3DZ7SvD/?ref=app
5th Jun 2022, 7:28 AM
X33X
X33X - avatar
0
You can use the reverse() method on the list.
5th Jun 2022, 12:43 AM
Justice
Justice - avatar
0
I tried the reverse method on my code i still got the exact same input in int mot reversed For the X input the result should be a reversed list of the input e.g [1,2,3,4] = [4,3,2,1] And i tried all three options x = input().split() # makes your list y = [int(i) for i in x] print(y[::-1]) Output = the same input unreversed e.g [1,2] is still [1,2] b = [] for i in x: b.append(int(i)) print(b[::-1]) Output= no output. or c = [] for i in range(len(x),0,-1): c.append(i) print(c) Output = e.g input = [1,2,3] it prints [‘123’, 1] None worked dont know whats wrong
5th Jun 2022, 3:34 AM
X33X
X33X - avatar
0
My input is 12345 i didn’t separate my input with commas
5th Jun 2022, 3:42 AM
X33X
X33X - avatar
0
Putting spaces in between the number works but the project sepcifies the user input all number without spaces its the credit card validator on sololearn It should read it all as a str first the we can reverse the str and turn that into an int (that was my idea)
5th Jun 2022, 3:56 AM
X33X
X33X - avatar
0
y should be of the same case eg both should either be of capital leter or small letter and not mixing
6th Jun 2022, 5:29 AM
DEMETRIUS NDIBALEMA
DEMETRIUS NDIBALEMA - avatar
0
Do nothing ; just “list_name.reverse”
6th Jun 2022, 8:33 PM
Radhakrishan
Radhakrishan - avatar
0
X =[1,2,3,5,3 8,4] Print(X.reverse())
6th Jun 2022, 10:15 PM
JUDE DANIA
JUDE DANIA - avatar