How to convert each item of a number or a string into an item of a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to convert each item of a number or a string into an item of a list

Lyk....123 to [1,2,3]

18th Jun 2019, 12:24 PM
Y AD Ù
Y AD Ù - avatar
4 Answers
+ 6
For string just do: my_list=list(string) For numbers first convert number to string.In your example: my_list=list(str(123))
18th Jun 2019, 12:38 PM
ΛM!N
ΛM!N - avatar
+ 17
Let the string be str1='hello' and declare a list lst=[] Then run a loop for all string elements like this - for a in str1: And then append the individual element of string to the list like this - lst.append(a) Hope this helps !!!
18th Jun 2019, 12:36 PM
Nova
Nova - avatar
+ 13
Cbr✔[exams] But I think according to your method, there will a single element 'hello23d' in the list. But we want the output as each character or number as a single element. So list should be ['h','e','l','l,'o'].
22nd Jun 2019, 8:38 AM
Nova
Nova - avatar
+ 13
Yup, that would work 👍
22nd Jun 2019, 9:08 AM
Nova
Nova - avatar