You can just cover it in brackets.
a = 1234
print([a])
Or append()
b = []
b.append(a)
output
[1234]
Answer from Thavas Antonio on Stack Overflowpython - Converting integer to digit list - Stack Overflow
Convert NumPy array to Python list - Stack Overflow
python - Converting list of list of Int32 values to list of list of int values - Stack Overflow
Django rest framework how to serialize a list of strings?
You can't just pass a list of strings. JSON is basically like a python dictionary. You need to format your object as a dictionary e.g.
{"strings":[string1, string2, string3]}Each string above is a string object.
It's better to pass each string though with a dictionary keyword so it's easier to extract later, or nested dictionaries if you need nester data storage.
Serialising converts python objects to JSON but it has to be something that has similar properties to JSON in terms of structure. Python has that in dictionaries, so we feed it dictionaries. Read up on JSON and how it looks. You'll recognize the similarities instantly.
More on reddit.comConvert 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.
Use tolist():
>>> import numpy as np
>>> np.array([[1,2,3],[4,5,6]]).tolist()
[[1, 2, 3], [4, 5, 6]]
Note that this converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the "nearest compatible Python type" (in a list). If you want to preserve the numpy data types, you could call list() on your array instead, and you'll end up with a list of numpy scalars. (Thanks to Mr_and_Mrs_D for pointing that out in a comment.)
c = np.array([[1,2,3],[4,5,6]])
list(c.flatten())
Examples of a non-iterative conversion.
Use the following to keep the structure as an ndarray:
a.astype(int)
>>> array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Or even more simply, use this to convert the ndarray to a list:
a.tolist()
>>> [[1, 2, 3, 4], [5, 6, 7, 8]]
Background:
You mention that your list is of int32 values. As such, it's highly likely that the 'list' is actually a numpy.ndarray. Here is an example:
a = np.array([[1,2,3,4],[5,6,7,8]], dtype=np.int32)
>>> array([[1, 2, 3, 4],
[5, 6, 7, 8]], dtype=int32)
If this is the case, then converting the ndarray to a 'normal' list of int values is as simple as the two examples shown above.
This should work:
In [167]: [[int(x) for x in sublist] for sublist in test_list]
Out[167]: [[1, 2, 3, 4], [5, 6, 7, 8]]