Python Forum

Full Version: Picking out integers from a non-list variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Imagine there are two variables:

source=123456
selected=[]


Is there a way to append the first integer from the variable 'source' into the list variable 'selected'?

If not, then how can i separate all the integers in the variable 'source' and append them into the 'selected' list like this:

>>>print(selected)
>>>['1','2','3','4','5','6']


Thanks for any responses.
>>> source = 123456
>>> list(str(source))
['1', '2', '3', '4', '5', '6']
>>> str(source)[0]
'1'
>>>
You can transform an integer into a string with str(integer_to_convert_to_string).
You can then iterate through each character of the string.