Convert input into list/tuple. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Convert input into list/tuple.

How to convert input into list and tuple? Conversion of input to list/tuple. Thanks in advance.

14th Mar 2020, 5:14 PM
Tanmay Gupta
Tanmay Gupta - avatar
6 Answers
+ 6
[edited]: All code in attached file! You can achieve this with: inp = 'aaa1hhh2bbb3' #output = 123 res = [i for i in inp if i.isdigit()] # result is list ['1', '2', '3'] print(res) res1 = ''.join(res) # result is '123' (string) print(res1) res2 = int(res1) # result is 123 (integer) print(res2) # or in a short way: res1 = ''.join([i for i in inp if i.isdigit()]) # '123' (string) res2 = int(''.join([i for i in inp if i.isdigit()])) # 123 (int) https://code.sololearn.com/ch5tuyjHfQix/?ref=app
15th Mar 2020, 7:24 AM
Lothar
Lothar - avatar
+ 5
input function give you a str (string). If you want a list of words you can do this: x=input() y=x.split() Split() function split you string by space For example x= "aaa bbbb", so y = ['aaa', 'bbbb'] You can also split by another symbols like comma, slash etc.
14th Mar 2020, 5:22 PM
Kostia Gorbach
Kostia Gorbach - avatar
+ 4
Similarly as already explained, the input will be split by space ( or by any other separator). First sample generates a tuple, second example generates a list. lst1 = tuple(input('...: ').split(' ')) lst2 = list(input('...: ').split(' '))
14th Mar 2020, 6:30 PM
Lothar
Lothar - avatar
+ 4
Thanks Lothar🙏🙏
15th Mar 2020, 9:02 AM
Tanmay Gupta
Tanmay Gupta - avatar
+ 3
Kostia Gorbach Lothar Thanks for your answers..
15th Mar 2020, 3:02 AM
Tanmay Gupta
Tanmay Gupta - avatar
+ 3
I want to split single letters or numbers on the basis of their opposites. For eg. input = aaa1hhh2bbb3 So I want only the numbers from the input and remove all the letters. output = 123 Please suggest this method also
15th Mar 2020, 3:05 AM
Tanmay Gupta
Tanmay Gupta - avatar