Say I have the following lists:
a = 1
b = [2,3]
c = [4,5,6]
I want to concatenate them such that I get the following:
[1,2,3,4,5,6]
I tried the usual +
operator:
>>> a+b+c
Traceback [most recent call last]:
File "", line 1, in
TypeError: unsupported operand type[s] for +: 'int' and 'list'
This is because of the a
term. It is just an integer. So I convert everything to a list:
>>> [a]+[b]+[c]
[1, [2, 3], [4, 5, 6]]
Not quite what I'm looking for.
I also tried all the options in this
answer, but I get the same int
error mentioned above.
>>> l = [a]+[b]+[c]
>>> flat_list = [item for sublist in l for item in sublist]
Traceback [most recent call last]:
File "", line 1, in
File "", line 1, in
TypeError: 'int' object is not iterable
It should be simple enough, but nothing works with that term a
. Is there any way to do this efficiently? It doens't necessarily have to be pythonic.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The interconversion of data types is a problem that is quite common in programming. Sometimes we need to convert a single number to list of integers and we don’t wish to spend several lines of code doing it. Hence having ways to perform this task using shorthands comes in handy. Let’s discuss ways in which this can be performed.
Method #1: Using list comprehension
Itcan be used as a shorthand for the longer format of the naive method. In this method, we convert the number to a string and then extract each character and re-convert it to an integer.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
[
int
[x]
for
x
in
str
[num]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #2: Using map[] map function can be used to perform the following task converting each of the string converted numbers to the desired integer value to be reconverted to the list format.
Python3
num
=
2019
print
[
"The original number is "
+
str
[num]]
res
=
list
[
map
[
int
,
str
[num]]]
print
[
"The list from number is "
+
str
[res]]
Output
The original number is 2019 The list from number is [2, 0, 1, 9]
Method #3: Using enumerate function
Python3
n
=
2019
res
=
[
int
[x]
for
a,x
in
enumerate
[
str
[n]]]
print
[res]
Method: Using lambda function
Python3
n
=
2019
x
=
list
[
filter
[
lambda
i:[i],
str
[
2019
]]]
print
[x]
Output
['2', '0', '1', '9']
Given a List of Integers. The task is to convert them to List of Strings. Examples:
Input: [1, 12, 15, 21, 131] Output: ['1', '12', '15', '21', '131'] Input: [0, 1, 11, 15, 58] Output: ['0', '1', '11', '15', '58']
Method 1: Using map[]
Python3
list_int
=
[
1
,
12
,
15
,
21
,
131
]
list_string
=
map
[
str
, list_int]
print
[
list
[list_string]]
Output:
['1', '12', '15', '21', '131']
Example 2: Using list comprehension
Python3
list_string
=
[
1
,
12
,
15
,
21
,
131
]
output
=
[
str
[x]
for
x
in
list_string]
print
[output]
Output:
['1', '12', '15', '21', '131']
Method 3: Using iteration
Python3
list_string
=
[
1
,
12
,
15
,
21
,
131
]
list_int
=
[]
for
i
in
list_string:
list_int.append[i]
print
[list_int]
Output:
['1', '12', '15', '21', '131']
Method: Using enumerate function
Python3
lst
=
[
1
,
12
,
15
,
21
,
131
];l
=
[]
for
i,a
in
enumerate
[lst]:
l.append[
str
[a]]
print
[l]
Output
['1', '12', '15', '21', '131']