I am a beginner in python and I wanted to know how to put a variable's int into a list. For example: numbers = 121 into list = [1, 2, 1] Thanks in advance
CodePudding user response:
Use:
num = 123
my_list = [int(i) for i in str(num)]
print(my_list) # Outputs [1, 2, 3]
CodePudding user response:
You can also use map()
:
list(map(int, str(numbers)))
Click here to learn more about map
.
CodePudding user response:
If you just have a regular int
like your question says, you can do it this way with python's append()
function for list
.
i = 1
int_list = []
int_list.append(i)
print(int_list)
Output
[1]
j = 2
int_list.append(j)
print(int_list)
Output
[1,2]
Check this link for more info: https://www.w3schools.com/python/python_lists_add.asp