There's nothing that will automatically treat an int as if it's a list of one int. You need to check whether the value is a list or not:
(a if type(a) is list else [a]) + (b if type(b) is list else [b]) + (c if type(c) is list else [c])
If you have to do this often you might want to write a function:
def as_list(x):
if type(x) is list:
return x
else:
return [x]
Then you can write:
as_list(a) + as_list(b) + as_list(c)
Answer from Barmar on Stack OverflowThere's nothing that will automatically treat an int as if it's a list of one int. You need to check whether the value is a list or not:
(a if type(a) is list else [a]) + (b if type(b) is list else [b]) + (c if type(c) is list else [c])
If you have to do this often you might want to write a function:
def as_list(x):
if type(x) is list:
return x
else:
return [x]
Then you can write:
as_list(a) + as_list(b) + as_list(c)
You can use itertools:
from itertools import chain
a = 1
b = [2,3]
c = [4,5,6]
final_list = list(chain.from_iterable([[a], b, c]))
Output:
[1, 2, 3, 4, 5, 6]
However, if you do not know the contents of a, b, and c ahead of time, you can try this:
new_list = [[i] if not isinstance(i, list) else i for i in [a, b, c]]
final_list = list(chain.from_iterable(new_list))
Convert the integer to string first, and then use map to apply int on it:
>>> num = 132
>>> map(int, str(num)) #note, This will return a map object in python 3.
[1, 3, 2]
or using a list comprehension:
>>> [int(x) for x in str(num)]
[1, 3, 2]
There are already great methods already mentioned on this page, however it does seem a little obscure as to which to use. So I have added some mesurements so you can more easily decide for yourself:
A large number has been used (for overhead) 1111111111111122222222222222222333333333333333333333
Using map(int, str(num)):
import timeit
def method():
num = 1111111111111122222222222222222333333333333333333333
return map(int, str(num))
print(timeit.timeit("method()", setup="from __main__ import method", number=10000)
Output: 0.018631496999999997
Using list comprehension:
import timeit
def method():
num = 1111111111111122222222222222222333333333333333333333
return [int(x) for x in str(num)]
print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
Output: 0.28403817900000006
Code taken from this answer
The results show that the first method involving inbuilt methods is much faster than list comprehension.
The "mathematical way":
import timeit
def method():
q = 1111111111111122222222222222222333333333333333333333
ret = []
while q != 0:
q, r = divmod(q, 10) # Divide by 10, see the remainder
ret.insert(0, r) # The remainder is the first to the right digit
return ret
print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
Output: 0.38133582499999996
Code taken from this answer
The list(str(123)) method (does not provide the right output):
import timeit
def method():
return list(str(1111111111111122222222222222222333333333333333333333))
print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
Output: 0.028560138000000013
Code taken from this answer
The answer by Duberly González Molinari:
import timeit
def method():
n = 1111111111111122222222222222222333333333333333333333
l = []
while n != 0:
l = [n % 10] + l
n = n // 10
return l
print(timeit.timeit("method()", setup="from __main__ import method", number=10000))
Output: 0.37039988200000007
Code taken from this answer
Remarks:
In all cases the map(int, str(num)) is the fastest method (and is therefore probably the best method to use). List comprehension is the second fastest (but the method using map(int, str(num)) is probably the most desirable of the two.
Those that reinvent the wheel are interesting but are probably not so desirable in real use.
How to convert an integer to a list in python? - Stack Overflow
How to convert a single number into a single item list in python - Stack Overflow
How to convert an input (number+string) to list, without converting the number into a string?
unicode to string conversion in nested list?
I think that the simplest thing that you can do is:
mylist = map(int, raw_input('Enter the numbers: ').split(','))
But it's nearly the same that using a list comprehension.
You should use raw_input and convert to int with a list comprehension:
user_input = raw_input('Enter the numbers: ')
my_list = [int(i) for i in user_input.split(',')]
From the offical documentation: raw_input reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
This is just a special case:
list1 = []
You can put the lists contents between the brackets. For example:
list1 = [i]
mylist = [i]
This will create a list called mylist with exactly one element i. This can be extended to create a list with as many values as you want: For example:
mylist = [i1,i2,i3,i4]
This creates a list with four element i1,i2,i3,i4 in that order. This is more efficient that appending each one of the elements to the list.
For eg.
x=input()
list_num = list(x)
print(list_num)
Here, if i input the value of x as, say, 2BC. Then the output is ['2','B','C']
But I want to know if there's any way to keep the 2 as int and not change it to a string.